我有2个模型:Categories
和CategoryAdditionalAttributes
,它们具有Categories
的外键。
此外,我正在为CategoryAdditionalAttributes
创建一个内联表单集,但在测试阶段遇到了此错误:
models.py
class Categories(models.Model):
category_breadcrumb = models.CharField(max_length=512, unique=True)
class Meta:
app_label = 'assortment'
def __str__(self):
return self.category_breadcrumb
class CategoryAdditionalAttribute(models.Model):
category = models.ForeignKey(
Categories, on_delete=models.CASCADE, related_name='additional_attributes')
attribute_key = models.CharField(max_length=500)
attribute_value = models.CharField(max_length=1000)
class Meta:
app_label = 'assortment'
def __str__(self):
return "{}: {}".format(str(self.attribute_key), self.attribute_value)
def __unicode__(self):
return "{}: {}".format(str(self.attribute_key), self.attribute_value)
admin.py
class CategoryAdditionalAttributeInlineFormSet(BaseInlineFormSet):
class Meta:
model = CategoryAdditionalAttribute
fields = ['attribute_key', 'attribute_value']
def save_new_objects(self, commit=True):
saved_attributes = super(
CategoryAdditionalAttributeInlineFormSet, self).save_new_objects(commit)
if commit:
CategoryAdditionalAttributeService()
.insert_additional_attributes(saved_attributes, self.deleted_forms)
return saved_attributes
def save_existing_objects(self, commit=True):
saved_attributes = super(
CategoryAdditionalAttributeInlineFormSet, self).save_existing_objects(commit)
if commit:
CategoryAdditionalAttributeService()
.insert_additional_attributes(saved_attributes, self.deleted_forms)
return saved_attributes
class CategoryAttributeInline(admin.TabularInline):
model = CategoryAdditionalAttribute
formset = CategoryAdditionalAttributeInlineFormSet
extra = 1
class CategoriesAdmin(admin.ModelAdmin):
form = CategoriesForm
search_fields = ['category_breadcrumb']
inlines = [CategoryAttributeInline]
测试
class CategoryAdditionalAttributesTest(TestCase):
....
def test_insert_additional_attribute_success(self):
AttributeFormSet = inlineformset_factory(
Categories, CategoryAdditionalAttribute, form=CategoryAdditionalAttributeInlineFormSet)
data = {
'form-TOTAL_FORMS': '1',
'form-INITIAL_FORMS': '0',
'form-MAX_NUM_FORMS': '',
'form-0-category': self.test_category,
'form-0-attribute_key': 'a_room',
'form-0-attribute_value': 'Kamar Mandi'
}
test_attribute_formset = AttributeFormSet(data)
self.assertEqual(test_attribute_formset.is_valid(), True)
结果
Traceback (most recent call last):
File "/home/varian/.local/share/virtualenvs/f1-thanos- aRnD22rB/local/lib/python2.7/site-packages/mock/mock.py", line 1305, in patched
return func(*args, **keywargs)
File "/home/varian/Desktop/dekoruma/f1- thanos/thanos/apps/assortment/tests/test_admin.py", line 134, in test_insert_additional_attribute_success
test_attribute_formset = AttributeFormSet(data)
File "/home/varian/.local/share/virtualenvs/f1-thanos-aRnD22rB/local/lib/python2.7/site-packages/django/forms/models.py", line 885, in __init__
if self.form._meta.fields and self.fk.name not in self.form._meta.fields:
AttributeError: type object 'CategoryAdditionalAttributeForm' has no attribute '_meta'
任何帮助将不胜感激。