我有以下管理员设置,以便我可以同时添加/编辑用户及其个人资料.
class ProfileInline(admin.StackedInline):
"""
Allows profile to be added when creating user
"""
model = Profile
class UserProfileAdmin(admin.ModelAdmin):
"""
Options for the admin interface
"""
inlines = [ProfileInline]
list_display = ['edit_obj', 'name', 'username', 'email', 'is_active',
'last_login', 'delete_obj']
list_display_links = ['username']
list_filter = ['is_active']
fieldsets = (
(None, {
'fields': ('first_name', 'last_name', 'email', 'username',
'is_active', 'is_superuser')}),
)
ordering = ['last_name', 'first_name']
search_fields = ['first_name', 'last_name']
admin.site.register(User, UserProfileAdmin)
Run Code Online (Sandbox Code Playgroud)
问题是我在添加用户时需要配置文件内联表单中的两个字段.除非输入输入,否则内联表单不会验证.反正是否要求内联,以便它不能留空?
我有两个Django模型(Purchaser和LineItem),我通过库存管理界面管理.愚蠢的版本:
class Purchaser(models.Model):
firstname = models.CharField('First Name', max_length = 30)
lastname = models.CharField('Last Name', max_length = 30)
paymentid = models.IntegerField('Payment ID', unique = True)
class LineItem(models.Model):
purchaser = models.ForeignKey(Purchaser)
ship_first_name = models.CharField('Recipient First Name', max_length = 50)
ship_last_name = models.CharField('Recipient Last Name', max_length = 50)
Run Code Online (Sandbox Code Playgroud)
我在买方管理页面中将LineItems作为内联,并希望要求购买者至少有一个LineItem(即,除非他们添加了至少一个LineItem,否则不要让用户保存新的购买者).有干净的方法吗?我已经使用自定义modelForm设置了一些验证,但该方法仅处理Purchaser字段,而不涉及LineItems.建议吗?