Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# coding: utf-8
# Есть такой код. Сделайте что-нибудь с Item.get_options().
from django.db import models
class Item(models.Model):
# .....
def get_options(self):
topics = []
for topic in Topic.objects.all():
options = []
for option in Option.objects.filter(topic=topic):
try:
item_option = ItemOption.objects.get(item=self, option=option)
options.append({'title': option.title, 'value': item_option.value})
except ItemOption.DoesNotExist:
options.append({'title': option.title, 'value': None})
topics.append({'name': topic.name, 'options': options})
return topics
class Topic(models.Model):
name = models.CharField(max_length=255)
class Option(models.Model):
topic = models.ForeignKey(Topic)
title = models.CharField(max_length=255)
class ItemOption(models.Model):
item = models.ForeignKey(Item)
option = models.ForeignKey(Option)
value = models.CharField(max_length=20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.