Django: Why do some model fields clash with each other?

后端 未结 6 2309
南笙
南笙 2020-11-29 16:10

I want to create an object that contains 2 links to Users. For example:

class GameClaim(models.Model):
    target = models.ForeignKey(User)
    claimer = mod         


        
6条回答
  •  我在风中等你
    2020-11-29 16:40

    Sometimes you have to use extra formatting in related_name - actually, any time when inheritance is used.

    class Value(models.Model):
        value = models.DecimalField(decimal_places=2, max_digits=5)
        animal = models.ForeignKey(
            Animal, related_name="%(app_label)s_%(class)s_related")
    
        class Meta:
            abstract = True
    
    class Height(Value):
        pass
    
    class Weigth(Value):
        pass
    
    class Length(Value):
        pass
    

    No clash here, but related_name is defined once and Django will take care for creating unique relation names.

    then in children of Value class, you'll have access to:

    herdboard_height_related
    herdboard_lenght_related
    herdboard_weight_related
    

提交回复
热议问题