mfalcon
mfalcon

Reputation: 880

Add a create button for foreign keys

I have the following models.py file:

class Account(models.Model):
    name = models.CharField(max_length=30)
    user = models.ForeignKey(User, related_name='account_creator')

class Category(models.Model):
    name = models.CharField(max_length=30)
    account = models.ForeignKey(Account, related_name='categories')

class Transaction(models.Model):
    namee = models.CharField(max_length=30)
    ...
    category = models.ForeignKey(Category, related_name='transactions', blank=True, null=True)
    account = models.ForeignKey(Account, related_name='transactions')

In a view I've a modelform for the Transaction class, but the problem with it is that I can't add a category or an account from this form. I'd like to know how to add a "create button" to the view/form. The django admin does this pretty well but I can't find how to use it.

Upvotes: 0

Views: 465

Answers (1)

Bernhard Vallant
Bernhard Vallant

Reputation: 50796

The django admin wraps the widget used for inputting with a wrapper class, called RelatedFieldWidgetWrapper. I'm afraid you cannot really use it outside the admin, because it is bound very tightly to it (for generating the "add another" view).

Upvotes: 2

Related Questions