[英]django admin. Create parent with child in same form
I have two classes in my model.py 我的model.py中有两个类
class User(models.Model):
name = models.CharField()
phone = models.CharField()
# Other common fields
class Customer(User):
payment = models.CharField()
user__id = models.OneToOneField('User', db_column='id', primary_key=True)
class Company(User):
address = models.CharField()
user_id = models.OneToOneField('User', db_column='id', primary_key=True)
When I use the admin of Customer/Company it includes all User fields, that is perfect for me. 当我使用客户/公司的管理员时,它包含所有用户字段,这对我来说是完美的。 But this form of Customer/Company also includes a dropdown list with the foreign key of an User, and I don't want to create the User first and then the Customer/Company object. 但是这种形式的客户/公司还包括一个带有用户外键的下拉列表,我不想先创建用户,然后再创建客户/公司对象。 I want that when I fill the Customer/Company form (with User fields) I should create the User object too. 我希望当我填写客户/公司表单(使用用户字段)时,我也应该创建用户对象。
Is there a way to solve this without create a User instance prior the Customer/Company instance? 有没有办法解决这个问题而不在Customer / Company实例之前创建User实例?
Thanks 谢谢
You can do this by adding an inline in your model registration in admin.py
. 您可以通过在admin.py
中的模型注册中添加内联来完成此admin.py
。
class AInline(admin.TabularInline):
model = A
class BAdmin(admin.ModelAdmin):
inlines = [AInline]
admin.register(A)
admin.register(B, BAdmin)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.