开发者

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开发者_Python百科 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.


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜