[Answer]-Django forms – Access fields of a foreign key

1πŸ‘

βœ…

You can restrict the inline fields like this (ofcourse you still need the form to validate, you could use javascript or default values to set gaps)

class MyModelInline(admin.TabularInline):
    model = MyModel
    fields = ["x", "y", "z"]

    #fk_name = "..."
    #max_num = 1
    #extra = 0

0πŸ‘

If you want to show proper text for β€˜B’ in choice field, in your model B add __unicode__ method and return string using fields of B,

eg.

class B(models.Model):
    a = models.IntegerField(...)
    b = models.CharField(...)
    c = models.BooleanField(...)
    def __unicode__(self):
         return u''+str(self.a) + ':' + self.b
πŸ‘€Rohan

0πŸ‘

inlineformset_factory accepts form argument, which is a modelform class for your β€œB” objects. So, defining a form class with fields = ('a', 'b') in Meta and passing it to the function should help.

πŸ‘€uruz

Leave a comment