FieldDoesNotExist:product_purchase_categories没有名为'无'多对多

时间:2018-02-08 15:03:41

标签: python django many-to-many

我正在尝试将ManyToMany关系添加到实例中,并且Django继续抛出一个奇怪的错误。我希望有人之前已经看过这个,或者可能对我为什么会看到这个有所了解。以下是我正在使用的两个型号。我的产品型号非常大,所以我把它缩小了。其他字段不应该有任何相关性。

class baseModel(models.Model):
    created_date = models.DateTimeField(auto_now_add=True,blank=True)
    updated_date = models.DateTimeField(auto_now=True,blank=True)
    inactive = models.BooleanField(default = False,blank=True)

    class Meta:
        abstract = True


class category(baseModel):
name = models.CharField(max_length=255, help_text="",blank=True)
short_name = models.CharField(max_length=255, help_text="",blank=True)
external_id = models.IntegerField(help_text="",blank=True,null=True)
sales_channel = models.ForeignKey('sales_channel',related_name = 'sales_categories',null=True,blank=True,on_delete=models.SET_NULL)
purchase_channel = models.ForeignKey('purchase_channel',related_name = 'purchase_categories',null=True,blank=True,on_delete=models.SET_NULL)
parent_category = models.ForeignKey('self',related_name='child_categories',null=True,blank=True,on_delete=models.SET_NULL)
category_level = models.IntegerField(null=True,blank=True)

def _product_count(self):
   "Number of Products"
   product_count = self.category_products.count()
   return product_count
product_count = property(_product_count)

def __str__(self):
    return self.name# + ' ('+str(self.product_count)+')'


class product(baseModel):
title = models.CharField(max_length=255, help_text="The title of the product.")
additional_details = models.CharField(max_length=255, help_text="Additional product information provided by the supplier",null=True,blank=True,)
description = models.TextField(max_length=4000, help_text="")
# ...
purchase_channel = models.ForeignKey('purchase_channel',related_name = 'products',null=True,blank=True,on_delete=models.SET_NULL)
purchase_external_id = models.IntegerField(help_text="external_id from purchasing channel",null=True,blank=True)
purchase_cost = models.DecimalField(max_digits=9,decimal_places=2, help_text="pruchase cost of the product.", blank=True, null=True)
purchase_categories = models.ManyToManyField(category,related_name = 'categories_products',blank=True)
# ...
def __str__(self):
    return self.title

以下是我在shell中运行的内容。抓住一个产品,抓住一个类别,尝试将所述类别添加到产品中,然后收到以下错误。

In [44]: prod = product.objects.get(id=2705)

In [45]: prod
Out[45]: <product: Embassy&trade; Italian Stone&trade; Design Genuine Leather Backpack/Cart>

In [46]: cat = category.objects.get(id=324)

In [47]: cat
Out[47]: <category: Accessories>

In [48]: prod.purchase_categories.add(cat)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.4/dist-packages/django/db/models/options.py in get_field(self, field_name)
    565             # field map.
--> 566             return self.fields_map[field_name]
    567         except KeyError:

KeyError: None

During handling of the above exception, another exception occurred:

FieldDoesNotExist                         Traceback (most recent call last)
<ipython-input-48-f73a74ace2f4> in <module>()
----> 1 prod.purchase_categories.add(cat)

/usr/local/lib/python3.4/dist-packages/django/db/models/fields/related_descriptors.py in __get__(self, instance, cls)
    496             return self
    497 
--> 498         return self.related_manager_cls(instance)
    499 
    500     def _get_set_deprecation_msg_params(self):

/usr/local/lib/python3.4/dist-packages/django/db/models/fields/related_descriptors.py in __init__(self, instance)
    779             self.reverse = reverse
    780 
--> 781             self.source_field = self.through._meta.get_field(self.source_field_name)
    782             self.target_field = self.through._meta.get_field(self.target_field_name)
    783 

/usr/local/lib/python3.4/dist-packages/django/db/models/options.py in get_field(self, field_name)
    566             return self.fields_map[field_name]
    567         except KeyError:
--> 568             raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name))
    569 
    570     def get_base_chain(self, model):

FieldDoesNotExist: product_purchase_categories has no field named 'None'

非常感谢任何帮助,并提前感谢您。

0 个答案:

没有答案