我正在尝试从ForeignKey关系模型访问'Meta'排序,但无法使其正常工作。以下是模型,我需要将子模型的顺序命名为父模型的功能。我可以在shell中访问Product或Product图像的订购但不能使用关系子模型进行订购。请指教。
Product(models.Model): #parent model
....
class Meta:
ordering = ['-date_created']
def primary_image(self):
images = self.images.all()
ordering = self.images.model._meta.ordering #Just need to get order of submodel Meta here.
ProductImages(models.Model): #submodel and images with related name "images".
product = models.ForeignKey(
'catalogue.Product', related_name='images', verbose_name=_("Product"))
original = models.ImageField(
_("Original"), upload_to=settings.MARKET_IMAGE_FOLDER, max_length=255)
caption = models.CharField(_("Caption"), max_length=200, blank=True)
#: Use display_order to determine which is the "primary" image
display_order = models.PositiveIntegerField(
_("Display order"), default=0,
help_text=_("An image with a display order of zero will be the primary"
" image for a product"))
class Meta:
ordering = ('display_order')
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
164. response = response.render()
File "C:\Python27\lib\site-packages\django\template\response.py" in render
158. self.content = self.rendered_content
File "C:\Python27\lib\site-packages\django\template\response.py" in rendered_content
135. content = template.render(context, self._request)
File "C:\Python27\lib\site-packages\django\template\backends\django.py" in render
74. return self.template.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
210. return self._render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in _render
202. return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
905. bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render
135. return compiled_parent._render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in _render
202. return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
905. bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render
135. return compiled_parent._render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in _render
202. return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
905. bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render
65. result = block.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
905. bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render
65. result = block.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
905. bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render
65. result = block.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
905. bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python27\lib\site-packages\django\template\defaulttags.py" in render
329. return nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
905. bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python27\lib\site-packages\django\template\defaulttags.py" in render
217. nodelist.append(node.render(context))
File "C:\Python27\lib\site-packages\django\template\defaulttags.py" in render
329. return nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
905. bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python27\lib\site-packages\django\template\defaulttags.py" in render
217. nodelist.append(node.render(context))
File "C:\Python27\lib\site-packages\django\template\base.py" in render
1197. return func(*resolved_args, **resolved_kwargs)
File "C:\Users\Mudassar\supermarekt\market\catalogue\templatetags\product_tags.py" in render_product
28. return template_.render(context)
File "C:\Python27\lib\site-packages\django\template\backends\django.py" in render
74. return self.template.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
212. return self._render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in _render
202. return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
905. bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render
56. result = self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
905. bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render
56. result = self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
905. bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python27\lib\site-packages\django\template\defaulttags.py" in render
574. six.iteritems(self.extra_context)}
File "C:\Python27\lib\site-packages\django\template\defaulttags.py" in <dictcomp>
573. values = {key: val.resolve(context) for key, val in
File "C:\Python27\lib\site-packages\django\template\base.py" in resolve
648. obj = self.var.resolve(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in resolve
789. value = self._resolve_lookup(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in _resolve_lookup
849. current = current()
File "C:\Users\AliKhan\amazonclone\market\catalogue\models.py" in primary_image
404. ordering = self.images.model.Meta.ordering
Exception Type: AttributeError at /offer/
Exception Value: type object 'ProductImage' has no attribute 'Meta'
答案 0 :(得分:1)
默认情况下,QuerySet返回的结果按模型Meta中的排序选项给出的排序元组排序。
所以你根本不需要访问它,他们已经使用了这个顺序。
如果您希望将相关模型订购作为其他模型的一部分进行应用,您应该能够将其包含在订购列表中
ordering = ['-date_created', 'images']