django-models

is there a difference between MyModel.objects.filter(pk=1) and MyModel.objects.get(pk=1) ?

Is there a difference in the result between: MyModel.objects.filter(pk=1) and MyModel.objects.get(pk=1) If there is no difference, then why does the .get() method exist? ...

Django: filtering models on the difference of Counts of related models

I've got a bunch of Order objects, each connected to one or more OrderRow objects (visible as a reverse relation to their parent Orders with Order.order_rows.all()). Each OrderRow has a collection_status attribute, which can be 'collected', 'uncollected' or a bunch of other special values. Each Order has a status attribute, one of the v...

DWH style query in Django to generate summary report at specified levels of detail

I have a catalog of items like so: class Items(models.Model): name=models.CharField() type=models.ForeignKey(ItemType) quantity=models.IntegerField() price=models.DecimalField() # Simplified [... other fields] and it has some attributes governed by: class ItemAttributes(models.Model): name=models.CharField() ...

In Django's multi-table inheritance, can the upload_to argument be overridden?

For the following code: class Image(models.Model): alt_name = models.CharField(max_length=200) url = models.CharField(max_length=255, blank=True) class Button(Image): source = models.ImageField(max_length=1024, upload_to='buttons') class Snapshot(Image): source = models.ImageField(max_length=1024, upload_to='snapshots') c...

Representing location data in Django Models

I'm having trouble deciding on how exactly to represent location data in a project I'm starting. The data is essentially a floor plan, with things existing at points. My first approach was to create a Point class which things will ForeignKey to. However, I quickly realized several problems. The first is that I need a way to represent...

Django Admin doesn't show entries.

If I create a new entry for one particular model it doesn't show up in the django admin. The Agency Model is causing the trouble. # catalog.models class Content(models.Model): class Meta: abstract = True BUNDESLAND_CHOICES = ( ('bw', 'Baden-Württemberg'), ('by', 'Bayern'), ('be', 'Berlin'), ...

How to implement objects modification trace

Hi there, With django admin, we have an history of who altered an object and when. I would like to add an "old value", "new value" to this to be able to roll back if needed. Plus I would like every modification made to my objects (also outside of admin) to be recorded as well. The final objective is to be able to trace every modificat...

Best way to ensure that at least one field in a model is specified?

I have a model with four fields that all have null=True defined. I'd like to prevent creation of an entirely null model; I'd rather not use a form validator since I want this to work with the default admin. If I override the save() method to do a check, what do I raise on an error? ...

Django Many To Many Insert Ordering

Hi everyone, We have been struggling with this for a few days and have done lots of searches on the web. We are trying to figure out how entries are saved in Django forms for many to many fields. For example we have a news model that has a many to many relationship with images. When we add images to a news article e.g. images with id ...

How to restrict models foreign keys to foreign objects having the same property

Here is my example : We have printers. We can define page formats that are linked to a specific printer then we define workflows that select a starting format (first page added to the printing job), a body format and an end format (last page added to the printing job). Start and End are not required (null and blank = True) I want to b...

Refactor large models.py file in Django app.

After reading monokrome's answer to Where should Django manager code live?, I've decided to split a large models.py into smaller, more manageable files. I'm using the folder structure foodapp/ models/ __init__.py #contains pizza model morefood.py #contains hamburger & hotdog models In __init__.py, I import the mode...

Filtering the Aggregate in the Django ORM

I have a function that looks like this: def post_count(self): return self.thread_set.aggregate(num_posts=Count('post'))['num_posts'] I only want to count posts that have their status marked as 'active'. Is there an easy way to add a filter before the Count function? Model Definitions: class Category(models.Model): name =...

How i can get the class of a model in a widget through ModelForms in Admin?

i need make a special widget for ForeignKeys in Admin, but i need get the class of model in the widget, somebody know how i can do it? I think the Widget have a Field, and Field have a ModelForm, and obviously ModelForm have a Model, but i need this model in a widget in the admin. ...

How do I add a temporary field to a model in Django?

I'm developing a web-app where the user can create multiple related elements and then save those elements to a database. The webapp has no idea of what the primary keys will be in the database so it assigns each element a UUID. These UUIDs are saved with each element in the database. When the webapp sends the data to the webserver to ...

Sorting products after dateinterval and weight

What I want is to be able to get this weeks/this months/this years etc. hotest products. So I have a model named ProductStatistics that will log each hit and each purchase on a day-to-day basis. This is the models I have got to work with: class Product(models.Model): name = models.CharField(_("Name"), max_length=200) slug = models...

Choicefield render does not distinct between empty string or Decimal('0')

Hi all, I've got a form that renders a choicefield widget with a total of 3 choice values: 0, 6, 19 (these are of type "Decimal'). When editing an object via a modelform that has the value 6 or 19 the widget has selected the proper one when rendered, but when the object is stored with the value "Decimal('0') it has selected the empty_...

How to properly remove a specific ManyToMany relationship?

I have a ManyToMany relationship with one of my Models. On deleting a child, I want to remove the relationship but leave the record as it might be being used by other objects. On calling the delete view, I get an AttributeError error: Exception Value: 'QuerySet' object has no attribute 'clear' This is my models.py: class Feed(...

Is there any way to make Django's get_or_create() to create an object without saving it to database?

When using Django's get_or_create(), when created=True, is there any way to make it so that it creates an object without saving it to DB? I want to take the newly created object, do some validation tests, and only save it to DB if it passes all tests. ...

Validate that atleast one modelfield has value in Django admin

Given the following model, how do I require that atleast one of the two fields has been given a value? class ZipUpload(models.Model): zip_file = models.FileField(upload_to="/tmp", blank=True, help_text='Select a file to upload.') zip_file_path = models.FilePathField(path="/tmp", blank=True, ...

How can I sort by the id of a ManyToManyField in Django?

I've got a ManyToManyField in a user object and it's used to map the users that user is following. I'm trying to show a subset list of who they have most recently followed. Is there a trick in .order_by() that will allow me to sort by the id of the ManyToManyField? The data is there, right? # (people the user is following) following = m...