django-models

How to use generic foreign key in custom m2m relationship model

In first version we can use custom table for ManyToManyField with parameter through=MyModel. MyModel should include foreign keys. But I want to use generic foreign key: content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey('content_type', 'object_id') content...

How do I create a Django queryset equivalent to a SQL query using the OR operator?

In Django, I know using filter with multiple arguments gets translated into SQL AND clauses. From the Django Book: You can pass multiple arguments into filter() to narrow down things further: >>> Publisher.objects.filter(country="U.S.A.", state_province="CA") [<Publisher: Apress>] Those multiple arguments get translated ...

how to create a tariff calculator in django?

basically i'm creating a tariff calculator for a utility company in my country, based on a progressive rates table, an example given below 2.(a) In any one month for less than 1,001 gallons a fee of ………………………..… $9.23 (b) Proportionate fee for every gallon thereafter up to 2,000 gallons inclusive at the rate per 1,000 gallons of ………………...

Adding a field to an existing Django model

Hi I have a little website with a few users and I'd like to add a field to one of my models (and hence tables.) The problem is that syncdb of course won't touch the existing models and I'm trying to figure out how to add this field to the model/table without having to repopulate the entire thing. Example: class Newspaper(models.Model...

Tune queryset.get for working without DoesNotExist exception in model's Manager

I have for two similar models with one common Manager, that returns queryset for current logged user. I want User_Places.current_user.get(place=XXX) returns existing User_Place() object or empty User_Places(), when DoesNotExist exception raised. How is it possible? CurrentUserManager doesn't know anything about User_Places and User_Proje...

ModelForms in Django where the underlying model depends on another model (via OneToOneField)

I have two models in my Django application, for the purposes of storing search parameters used for some homology search programs: # models.py class Search(models.Model): """A class to represent search runs.""" program = models.CharField(max_length=20) results_file = models.FileField( upload_to=(SEARCH_RESULTS_DIR) ...

'Answer' instance needs to have a primary key value before a many-to-many relationship can be used.

I have a model called Answer which has a ForeignKey relationship to another model called Question. This means there can be several answers to question, naturally. class Question(models.Model): kind = models.CharField(max_length=1, choices=_SURVEY_QUESTION_KINDS) text = models.CharField(max_length=256) class Answer(models.Model...

Django -- ForeignKey() uses different column than the row ID?

Hi, I have an application called Location. Location has Country, State, City, Big_City_Nearby, Longitude, latitude. I have an application for an item for sell. Item has Title, Link, Description, Price & Location which is a ForeignKey(). Now, if someone wants to see all items for sell in the US, they click on a link (let say http://ex...

Django filter -- How do I go about filtering for emply or NULL names in a queryset

Hi, I have first_name, last_name & alias (optional) which I need to search for. So, I need a query to give me all the names that have an alias set. Only if I could do: Name.objects.filter(alias!="") So, what is the equivalent to the above? Thanks, VN44CA ...

how to do a select max in django

I have a list of objects how can I run a query to give the max value of a field: I'm using this code: def get_best_argument(self): try: arg = self.argument_set.order_by('-rating')[0].details except IndexError: return 'no posts' return arg rating is an integer ...

how can I change to way a boolean prints in a django template

I have some django code that prints a BooleanField it is rendered as True or False, can I change the label to be Agree/Disagree or do I need to write logic for that in the template? ...

Django select_related() for multi-join query using model & filters

Hi, I have two Models: Job & Location: class Job(models.Model): title = models.CharField(max_length=20) company = models.CharField(max_length=20) location = ForeignKey('Location') class Location(models.Model): country = models.CharField(max_length=20) state = models.CharField(max_length=20) city = models.CharField(m...

Modelling a swiss tournament in Django

I'm trying to create models that represent a swiss tournament, with multiple rounds. Each round everyone will be paired up with another player, except in the case where there is an odd player out, when one player will get a bye. I need to keep track of the outcome of each pairing; i.e., which player won. Also, I'd like to be able to ef...

is it safe to refactor my django models?

My model is similar to this. Is this ok or should I make the common base class abstract? What are the differcenes between this or makeing it abstract and not having an extra table? It seems odd that there is only one primary key now that I have factored stuff out. class Input(models.Model): details = models.CharField(max_len...

How to limit the maximum value of a numeric field in a Django model?

Django has various numeric fields available for use in models, e.g. DecimalField and PositiveIntegerField. Although the former can be restricted to the number of decimal places stored and the overall number of characters stored, is there any way to restrict it to storing only numbers within a certain range, e.g. 0.0-5.0 ? Failing that, ...

Object label in GAE Django

In my usual django code I use the unicode function to give each object a label...this appears in the admin interface as the oject label in the objects listed... class apprd(models.Model): usr = models.ReferenceProperty(User) approved = models.BooleanProperty(default = True) def __unicode__(self): return ('approved one') ...

Django -- User.DoesNotExist does not exist?

I'm trying to get hold of Django. I use Pydev on Eclipse. I have written a simple signup page that I can't get to work. Eclipse complains that User.DoesNotExist is undefined. Most likely, I am missing something trivial. Here's the relevant portion of the code: from django.contrib.auth.models import User ... class SignUpForm (forms.Form)...

Django ImageField issue

I have a similar model Class Student(models.Model): """A simple class which holds the basic info of a student.""" name = models.CharField(max_length=50) age = models.PositiveIntegerField() photo = models.ImageField(upload_to='foobar', blank=True, null=True) As we can see photo field is optional. I wanted all the students who have the...

Problems with contenttypes when loading a fixture in Django

I am having trouble loading Django fixtures into my MySQL database because of contenttypes conflicts. First I tried dumping the data from only my app like this: ./manage.py dumpdata escola > fixture.json but I kept getting missing foreign key problems, because my app "escola" uses tables from other applications. I kept adding addition...

Django BooleanField as radio buttons?

Is there a widget in Django 1.0.2 to render a models.BooleanField as two radio buttons instead of a checkbox? ...