我已多次发布此问题,但无法找到解决问题的方法。
下面,我的代码在Django 1.6.3中运行良好,但在更新到1.9.6之后它无法正常工作,尽管我已经更新了所有依赖项并进行了所有数据库迁移。
我的错误是:
for color_group in blank.color_groups.all():
指向view.py
以下是我的代码
Model.py
class ColorGroup(models.Model):
"""Categorization for Colors"""
name = models.CharField(max_length=100)
sort_order = models.IntegerField()
class Meta:
ordering = ['sort_order']
app_label='portal'
def __unicode__(self):
return self.name
class Blank(models.Model):
"""
A particular instance of a printable item, such as the Black Men's
American Apparel 50/50 t-shirt.
"""
image = models.ImageField(upload_to="blank_images", blank=True, validators=[validate_file_extension])
color = models.CharField(max_length=100)
style = models.ForeignKey(BlankStyle)
is_featured = models.BooleanField(verbose_name="Featured?", default=False)
color_groups = models.ManyToManyField(ColorGroup, related_name='blanks')
def as_hash(self):
image_url = ""
if self.image:
image_url = self.image.url
return {
'id': self.id,
'color': self.color,
'style_code': self.style.code,
'style': self.style.as_hash(),
'image_url': image_url,
'sizes': [bs.as_hash() for bs in self.sizes.all()]
}
@property
def image_url(self):
if self.image:
return self.image.url
return None
@property
def public_thumbnail_url(self):
from sorl.thumbnail import get_thumbnail
if self.image:
im = get_thumbnail(self.image, '58x58', crop='center',quality=99)
return im.url
return None
@property
def public_medium_url(self):
from sorl.thumbnail import get_thumbnail
if self.image:
im = get_thumbnail(self.image, '382x456',quality=99)
return im.url
return None
def __unicode__(self):
return self.style.code + ": " + self.color
class Meta:
app_label = 'portal'
和view.py
def order_new_json_data(request,all=False):
# Brands
brands = Brand.objects.all()
json_brands = serializers.serialize("json", brands)
# Blanks
blanks = Blank.objects.prefetch_related('style').filter(is_featured=True).order_by('style__id')
# custom output
blank_list = []
for blank in blanks.all():
color_groups = []
for color_group in blank.color_groups.all():
color_groups.append(color_group.pk)
回溯:
File "/home/muhammad/Desktop/ClockwiseLatest/local/lib/python2.7/site- packages/django/core/handlers/base.py" in get_response 149. response = self.process_exception_by_middleware(e, request)
File "/home/muhammad/Desktop/ClockwiseLatest/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/muhammad/Desktop/ClockwiseLatest/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view 149. response = view_func(request, *args, **kwargs)
File "/home/muhammad/Desktop/ClockwiseLatest/clockwise/public/orders/views.py" in order_new_json_data 356. for color_group in blank.color_groups.all():
File "/home/muhammad/Desktop/ClockwiseLatest/local/lib/python2.7/site-packages/django/db/models/manager.py" in all 223. return self.get_queryset()
File "/home/muhammad/Desktop/ClockwiseLatest/local/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.py" in get_queryset 792. return qs._next_is_sticky().filter(**self.core_filters)
File "/home/muhammad/Desktop/ClockwiseLatest/local/lib/python2.7/site-packages/django/db/models/query.py" in filter 790. return self._filter_or_exclude(False, *args, **kwargs)
File "/home/muhammad/Desktop/ClockwiseLatest/local/lib/python2.7/site-packages/django/db/models/query.py" in _filter_or_exclude 808. clone.query.add_q(Q(*args, **kwargs))
File "/home/muhammad/Desktop/ClockwiseLatest/local/lib/python2.7/site-packages/django/db/models/sql/query.py" in add_q 1243. clause, _ = self._add_q(q_object, self.used_aliases)
File "/home/muhammad/Desktop/ClockwiseLatest/local/lib/python2.7/site-packages/django/db/models/sql/query.py" in _add_q 1269. allow_joins=allow_joins, split_subq=split_subq,
File "/home/muhammad/Desktop/ClockwiseLatest/local/lib/python2.7/site-packages/django/db/models/sql/query.py" in build_filter 1149. lookups, parts, reffed_expression = self.solve_lookup_type(arg)
File "/home/muhammad/Desktop/ClockwiseLatest/local/lib/python2.7/site-packages/django/db/models/sql/query.py" in solve_lookup_type 1035. _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
File "/home/muhammad/Desktop/ClockwiseLatest/local/lib/python2.7/site-packages/django/db/models/sql/query.py" in names_to_path 1330. "Choices are: %s" % (name, ", ".join(available)))
Exception Type: FieldError at /orders/new/json_data/
Exception Value: Cannot resolve keyword u'blanks' into field. Choices are: id, name, sort_order