django-models

Django signals file, cannot import model names

怎甘沉沦 提交于 2020-01-02 05:24:11
问题 I have such file order: project/ app/ models.py signals.py I am keeping signals inside signals.py as it should be. and at the top of the signals.py file, I include myapp models as I do queries in these signals with from myproject.myapp.models import Foo However it doesnt seem to find it, as I run the server or validate from manage.py, it gives this error: from myproject.myapp.models import Foo ImportError: cannot import name Foo I am using Django 1.2.1. 回答1: Most likely you have a circular

Using __new__ on classes derived from Django's models does not work

元气小坏坏 提交于 2020-01-02 03:40:07
问题 This is something which is puzzling me, but I cannot get a definitive answer. Using the __new__ method (or more accurately, static method) within classes derived from DJango model. This is how __new__ should be ideally used (since we are using Django, we can assume that version 2.x of python is being used): class A(object): def __new__(self, *args, **kwargs): print ("This is A's new function") return super(A, self).__new__(self, *args, **kwargs) def __init__(self): print ("This is A's init

Haystack search on a many to many field is not working

巧了我就是萌 提交于 2020-01-02 03:29:33
问题 I'm trying to run a search on a model that has a many to many field, and I want to filter the search using this field. here is my current code: search_indexes.py class ListingInex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) business_name = indexes.CharField(model_attr='business_name') category = indexes.MultiValueField(indexed=True, stored=True) city = indexes.CharField(model_attr='city') neighborhood= indexes.CharField(model_attr=

Django - Limit choices to something that depends on the instance

泪湿孤枕 提交于 2020-01-02 02:20:30
问题 I have some foos which are organized into categories. For each category, I want to be able to select a winner foo. Hence I have models which look like this: class Category(models.Model): name = models.CharField(max_length=30) # More fields... winner = models.ManyToManyField( 'Foo', related_name='winner' ) class Foo(models.Model): name = models.CharField(max_length=30) # More fields... category = models.ForeignKey( Category, related_name='category' ) (The reason why winner is a ManyToManyField

Queryset from a ManyToMany relation

此生再无相见时 提交于 2020-01-01 19:22:09
问题 I'm creating a little calendar app in Django. I have two model classes; Calendar and Event. An event can be in multiple calendars. Because of this I'm using a ManyToMany relation. This is my model from django.db import models class Calendar(models.Model): title = models.CharField(max_length = 255) def __unicode__(self): return self.title class Event(models.Model): title = models.CharField(max_length = 255) start_date = models.DateField() end_date = models.DateField(blank = True, null = True)

Django model instance specific code

一世执手 提交于 2020-01-01 19:09:22
问题 in one of my Django models I have to add specific code for each model instance. Now, I wonder what would be a good way to implement this. My current attempt results in a big, hard to read if statement. Consider the following model: class Foo(models.Model): name = models.CharField(max_length=255) def do_instance_specific_stuff(self, arg): if self.id == 1: do_X(arg) elif self.id == 2: do_Y(arg) else: do_Z() Right now, there is custom code for around 20 model instances and it will stay in this

Django model instance specific code

假装没事ソ 提交于 2020-01-01 19:09:02
问题 in one of my Django models I have to add specific code for each model instance. Now, I wonder what would be a good way to implement this. My current attempt results in a big, hard to read if statement. Consider the following model: class Foo(models.Model): name = models.CharField(max_length=255) def do_instance_specific_stuff(self, arg): if self.id == 1: do_X(arg) elif self.id == 2: do_Y(arg) else: do_Z() Right now, there is custom code for around 20 model instances and it will stay in this

Django - Model best practice when fields need to be repeated

一笑奈何 提交于 2020-01-01 19:00:13
问题 I have a model where some fields could be repeated between 0 and 5 times for the same object. models.py : class FusionTableLayer(models.Model): layer_name = models.SlugField(max_length=50) condition1 = models.CharField('SQL Query Conditions', max_length=100, blank=True) condition2 = models.CharField('SQL Query Conditions', max_length=100, blank=True) condition3 = models.CharField('SQL Query Conditions', max_length=100, blank=True) condition4 = models.CharField('SQL Query Conditions', max

Django OneToOne field to self

断了今生、忘了曾经 提交于 2020-01-01 17:29:20
问题 How to define OneToOne relationship to the same Model ? I have a model called Order which can be paired with another one Order . Now I'm trying to figure out how to handle models for this relationship. My ideas: class Order(models.Model): paired_order = models.OneToOneField(self) OR: class Pairing(models.Model): order1 = models.ForeignKey(Order, related_name='pairing') order2 = models.ForeignKey(Order, related_name='pairing') What do you think? Which is more efficient? I want to have simple

In Django, How to call the 'User Group' as a foreign key in a model class?

淺唱寂寞╮ 提交于 2020-01-01 17:25:53
问题 I created a model class named Question. I want only particular user group can answer the questions. NOW, How can I bring a foreign key here from user group? In my DB there is a Question Table. There will be a lot of questions. But not all the users are allowed to view or answer the questions. This will depend on in which user group the user belongs to. So I have to specify which user group is allowed to answer the question in the Question table. Now, I have to create a field in question table