Created
September 12, 2015 10:06
-
-
Save dmitry-mukhin/6ea8a473b9fe8b726215 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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