1

I am running a django app and have a setup like this:

ModelSuper(models.Model):
   class Meta:
     abstract = False

ModelSub1(ModelA):
   name = models.CharField(...)
   
   def __str__:
      return self.name

ModelSub2(ModelA)
   name = models.CharField(...)

   def __str__:
      return self.name



ModelForeign(models.Model):
   element = models.ForeignKey(ModelA)

    def __str__:
      return self.name

So ModelForeign has a FK to ModelSuper. What happens now is that when I create an instance of ModelForeign I can choose if it belongs either to ModelSub1 or to ModelSub2. But the string representation is ModelSuper Onject (3) where (3) is the id.

Normally I can change this representation by overwriting the __str__ method on the model, but since I do not have any fields on the Supermodel I can't return anything.

What I tried:

  • I have already implemented the __str__ method in the Submodels but that does not help.
  • I wanted to make the Super model abstract. But this does not let me point FKs to the Supermodel, so I can't do this. My setup requires this FK
  • I used a generic FK with django's ContentType framework. This is also not an option because it messes completely with my app and is also not recommended from an SQL perspective.

Also when I do API-calls I get ModelSuper Onject (3) back instead of a human-readable name.

Is there a way to do what I intend to do? Thanks in advance for help and hints. Very much appreciated!

EDIT1: What I tried thanks to Abdul's help:

class ModelA(models.Model):
    class Meta:
        abstract = False

    TYPE_CHOICES = [('sub1', 'sub1'), ('sub2', 'sub2')]
    type_model = models.CharField(max_length=50, choices=TYPE_CHOICES, null=True, blank=True)

    def __str__(self):
        if self.type_model == "sub1":
            return "sub1"
        elif self.type_model == "sub2":
            return "sub2"
        else:
            return "unkown"

1 Answer 1

1

I am not understanding how your foreign key works as model inheritance means the tables are separate. How about trying something like this:-

ModelA(models.Model):
   TYPE_CHOICES = [('Sub1', 'ModelSub1'), ('Sub2', 'ModelSub2')]
   model_type = models.CharField(max_length=4, choices=TYPE_CHOICES)
   def __str__:
      # Return string representation using if-else

   class Meta:
     abstract = False

ModelSub1(ModelA):
   name = models.CharField(...)
   model_a = models.ForeignKey(ModelA, on_delete=models.CASCADE)
   
   def __str__:
      return self.name

ModelSub2(ModelA)
   name = models.CharField(...)
   model_a = models.ForeignKey(ModelA, on_delete=models.CASCADE)

   def __str__:
      return self.name



ModelForeign(models.Model):
   element = models.ForeignKey(ModelA)

    def __str__:
      return self.name
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for your help. I tried what you suggested and added it to the question. Maybe to clarify: I never create instances of ModelA, but only of the Submodels. What I tried with your suggestion gives me unknown
Also, when I try to create a new instance of a submodel I then get __str__ returned non-string (type NoneType)... Weird... Any idea why that could be?
With this solution you have to create instances of ModelA Along with the Submodels. You can simply add a few lines in the views to do this for you when the instances of the Submodels are made. Also to make the relation you need to point a foreign key / OnetoOnefield from the submodels to the ModelA
do you have null=True on the CharField for name? If so try returning self.name or 'unknown'
Thank you again. hmmm I don't think it's an option for me to create an instance of ModelA every time I create a submodel... And I can't return self.name on the Superclass because I don't have a name field. And on the subclasses I already return self.name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.