开发者

Grouping model objects in Django

开发者 https://www.devze.com 2023-03-27 14:29 出处:网络
I have an app with 2 models: Product and Photo, each of which corresponds to a MySQL table (drived by MyISAM).

I have an app with 2 models: Product and Photo, each of which corresponds to a MySQL table (drived by MyISAM).

Product is a ForeignKey field of Photo. Several Photo objects may share a single Product object.

Now, the question: I need Photo objects to be further subgrouped into sets representing the same real world object (instance of a product) photographed from different aspects. I want to differentiate this different real world objects, but still have them all connected to their Product object.

What's the best way to group the photos in terms of efficiency of both d开发者_JAVA技巧atabase querying and manual data input?


Thanks to @shawnwall, more ideas:

  • Maybe Product should be connected not to individual Photo objects but to sets of photos.
  • There should be a set for each real-word object, even if there's only 1 photo of it now.
  • The set should be represented by an ID field on the Photo table, common between certain Photo objects and a Product object. (What kind of field is that?)


Seems like your approach is decent so far. I considered a manytomany field from product to photos, but it doesn't sound like its needed. You could add an 'aspect' column that relates to a list of 'choices' on the photo table. Also remember django will let you query both ways:

Photo.objects.filter(product__id=1)

or

Product.objects.filter(photo__id=2)

You can also access them through instances:

photo.product   

or

product.photo_set
0

精彩评论

暂无评论...
验证码 换一张
取 消