我是Django的新手,我有一个关于在生产代码中重用管理界面的内联formset的问题。考虑以下模型:
class Attribute(models.Model):
name = models.CharField(max_length=50)
class Person(models.Model):
lastName = models.CharField(max_length=50)
firstName = models.CharField(max_length=50)
email = models.CharField(max_length=50)
attributes = models.ManyToManyField(Attribute, through='AttributeValue')
class AttributeValue(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE)
attribute = models.ForeignKey(Attribute, on_delete=models.PROTECT)
isConsumer = models.BooleanField()
value = models.CharField(max_length=50)
这个管理界面很漂亮。我使用了以下admin.py:
class AttributeValueInline(admin.TabularInline):
model = AttributeValue
extra = 3
class PersonAdmin(admin.ModelAdmin):
list_display = ('lastName', 'firstName','email')
fieldsets = [
('Name', {'fields': ['firstName', 'lastName']}),
('Contact Info',{'fields': ['email','phoneNumber']})
]
inlines = [AttributeValueInline]
admin.site.register(Person, PersonAdmin)
admin.site.register(Attribute)
这几乎就是我想要的。它看起来像这样:
当我尝试在非管理员网站中实现相同的功能时,除了实际的业务逻辑之外,我还必须编写相当多的代码来执行内联表单集,表单处理。除此之外,我还遇到了here提到的问题。
我无法将管理界面暴露给最终用户,因为我们需要一个自定义的网页设计和页面流。但是,我可以重用部分管理界面代码而不是重新实现相同的功能吗?
欢迎任何其他减少代码的建议。
答案 0 :(得分:4)
以下是如何实现像Django admin这样的内联表单集的答案 http://kevindias.com/writing/django-class-based-views-multiple-inline-formsets/ 但是只描述了CreateWiew。如果你想同时实现UpdateView,你需要通过一些调整来复制你的代码
def get(self, request, *args, **kwargs):
"""
Handles GET requests and instantiates blank versions of the form
and its inline formsets.
"""
self.object = self.get_object()
form_class = self.get_form_class()
form = self.get_form(form_class)
sample_form = SampleFormSet(instance=self.object)
return self.render_to_response(
self.get_context_data(form=form,
sample_form=sample_form))
def post(self, request, *args, **kwargs):
"""
Handles POST requests, instantiating a form instance and its inline
formsets with the passed POST variables and then checking them for
validity.
"""
self.object = self.get_object()
form_class = self.get_form_class()
form = self.get_form(form_class)
sample_form = SampleFormSet(self.request.POST, instance=self.object)
if (form.is_valid() and sample_form.is_valid()):
return self.form_valid(form, sample_form)
else:
return self.form_invalid(form, sample_form)
def form_valid(self, form, sample_form):
"""
Called if all forms are valid. Creates a Recipe instance along with
associated Ingredients and Instructions and then redirects to a
success page.
"""
self.object = form.save()
sample_form.save()
return HttpResponseRedirect(self.get_success_url())
并且不要忘记将DELETE formset字段添加到模板
{% for form in sample_form %}
{{ form.id }}
<div class="inline {{ sample_form.prefix }}">
{{ form.description.errors }}
{{ form.description.label_tag }}
...
{{ form.DELETE }}
</div>
{% endfor %}