Suppose there are two classes:
class A(models.Model):
…
class B(models.Model):
a = models.ForeignKey(A)
…
For the convenience to add a B without remembering the A, how can I add a link to add a B in A’s change form?
Here is a solution mentioned in this link, which suggest adding some codes in B’s model, but I don’t think codes like that should be placed in model.
Update:
The inline has a problem(or I don’t do it right): when the number of inline objects exceeded the max_num attribute, there is no new blank form to add new objects. However, the number of inline objects could be very big.
Model A has a reverse foreign key to B, because B has a foreign key to A:
Django offers “inlines” to deal with reverse foreign keys.
In the context of forms in user defined views/your code, use inline model formsets.
In the context of Django admin, use a InlineModelAdmin like StackedInline or TabularInline
If you do not want the inline formset to be generated from existing objects, then override the queryset() method of your admin inline formset class, to return something like: YourModel.objects.none()
This will make the inline formset to be always empty. If you only want one extra form then set extra=1.