django-models

Django DB, finding Categories whose Items are all in a subset

I have a two models: class Category(models.Model): pass class Item(models.Model): cat = models.ForeignKey(Category) I am trying to return all Categories for which all of that category's items belong to a given subset of item ids (fixed thanks). For example, all categories for which all of the items associated with that catego...

django model structure for many to many relasionship based on third model

I currently have three models: class Request(models.Model): requester=models.CharField() dateRequested = models.DateField(auto_now=True) title= models.CharField(max_length=255) description = models.TextField() class RequestType(models.Model): requestType=models.CharField('Request Type', max_length=256) class Reques...

Django: How to merge two related querysets in Django 0.96?

Hi all. I would like to merge one querysets related objects with another querysets related objects. Some sample code to explain: ## Models # sample models to illustrate problem class PetShop(models.Model): id = models.AutoField(primary_key=True) shop_name = models.CharField(maxlength=255) cats = models.ManyToManyField(Cat) ...

Is there any ways to define 2-tuples in one line?

I'm not expierenced in python. I need to define field 'year' with range restriction. Now i'm using this code, but I think there exists shorten way to do this. YEAR_CHOICE = [] for year in range(2020,1899,-1): YEAR_CHOICE += [(year, year)] year = models.PositiveSmallIntegerField('Year', choices=YEAR_CHOICE, default=0) Is therу any w...

Django GROUP BY strftime date format

I would like to do a SUM on rows in a database and group by date. I am trying to run this SQL query using Django aggregates and annotations: select strftime('%m/%d/%Y', time_stamp) as the_date, sum(numbers_data) from my_model group by the_date; I tried the following: data = My_Model.objects.values("strftime('%m/%d/%Y', ...

forms.SelectMultiple from models.CommaSeparatedIntegerField

I have a model with field: class Movie(models.Model): genre = models.CommaSeparatedIntegerField(max_length=100, choices=GENRE_CHOICES, blank=True, default=0) lang = models.CommaSeparatedIntegerField(max_length=100, choices=LANG_CHOICES, blank=True, default=0) And I need to get multiple select fields (not checkboxes) from that....

Dynamic Meta attributes for Django Models?

I am trying to add a dynamic Meta attribute to all of my Django models using model inheritance, but I can't get it to work. I have a permission that I want to add to all my models like this: class ModelA(models.Model): class Meta: permisssions =(('view_modela','Can view Model A'),) class ModelB(models.Model): class M...

Django Model set foreign key to a field of another Model

Is there any way to set a foreign key in django to a field of another model? For example, imagine I have a ValidationRule object. And I want the rule to define what field in another model is to be validated (as well as some other information, such as whether it can be null, a data-type, range, etc.) Is there a way to store this field-...

Django Model: Returning username from currently logged in user

I'm working on a Django app for hosting media (specifically audio and images). I have image galleries and photos separate in my model, and have them linked with a ForeignKey (not sure if that's correct, but still learning). What I need is for the Album class's __unicode__ to return the album owner's username. class Album(models.Model): ...

Get Primary Key after Saving a ModelForm in Django

How do I get the primary key after saving a ModelForm? After the form has been validated and saved, I would like to redirect the user to the contact_details view which requires the primary key of the contact. def contact_create(request): if request.method == 'POST': form = ContactForm(request.POST) if form.is_valid(...

Selecting date formats for aggregate calculations from database with Django

I would like to do aggregate calculations based on month for a datetime field. I am currently using the extra() function to format the date like: ...extra(select="strftime('column', '%m/%Y') as t").values('t').annotate(SUM(foo)) and it works great for sqlite3. In sqlite3 I can use strftime(), but that doesn't work with MySQL. In MyS...

Customize/remove Django select box blank option

I'm using Django 1.0.2. I've written a ModelForm backed by a Model. This model has a ForeignKey where blank=False. When Django generates HTML for this form it creates a select box with one option for each row in the table referenced by the ForeignKey. It also creates an option at the top of the list that has no value and displays as ...

Fetching ManyToMany objects from multiple objects through intermediate tables

Is there an easy way to fetch the ManyToMany objects from a query that returns more than one object? The way I am doing it now doesn't feel as sexy as I would like it to. Here is how I am doing it now in my view: contacts = Contact.objects.all() # Use Custom Manager Method to Fetch Each Contacts Phone Numbers contacts = PhoneNumber.obje...

A Question About Nested Collections in Django and Query Efficiency

I see a lot of answers like this one: Printing a list of persons with more than one home each home with more than one I have tried that answer with similar models and it seems like a terribly inefficient way of doing this. Each iteration seems to make a separate query sometimes resulting in thousands of queries to a database. I realize...

How can I easily mark records as deleted in Django models instead of actually deleting them?

Instead of deleting records in my Django application, I want to just mark them as "deleted" and have them hidden from my active queries. My main reason to do this is to give the user an undelete option in case they accidentally delete a record (these records may also be needed for certain backend audit tracking.) There are a lot of for...

Group models in django admin

Is there any way to group the models in django admin interface? I currently have an app called requests with the following models showing in the admin site: **Requests** Divisions Hardware Requests Hardware Types Requests Software Requests Software Types I would like the divisions, Software Requests and Hardware Requests to be groupe...

How do you order lists in the same way QuerySets are ordered in Django?

I have a model that has an ordering field under its Meta class. When I perform a query and get back a QuerySet for the model it is in the order specified. However if I have instances of this model that are in a list and execute the sort method on the list the order is different from the one I want. Is there a way to sort a list of in...

Default value for field in Django model

Suppose I have a model: class SomeModel(models.Model): id = models.AutoField(primary_key=True) a = models.CharField(max_length=10) b = models.CharField(max_length=7) Currently I am using the defauly admin to create/edit objects of this type. How do I remove the field b from the admin so that each object cannot be created w...

How do you serialize a model instance in Django?

There is a lot of documentation on how to serialize a Model QuerySet but how do you just serialize to json the fields of a Model Instance? ...

filter for many to many field

so I have this model: class Message(models.Model): creator = models.ForeignKey(User, unique=True) note = models.CharField(max_length=200, blank=True) recipients = models.ManyToManyField(User, related_name="shared_to") read = models.ManyToManyField(User, related_name="read", blank=True) I want to filter on people who ar...