django-orm

Easiest way to write a Python program with access to Django database functionality

Hi I have a website which fetches information from RSS feeds periodically (well, currently manually, and this is my problem). This is currently implemented as a normal Django view, which isn't very nice in my opinion. I'd like to have a Python program which is run using a cronjob instead of manually visiting the correct URL to update ...

Django model inheritance, filtering models

Given the following models:(don't mind the TextFields there're just for illustration) class Base(models.Model): field1 = models.TextField() class Meta: abstract=True class Child1(Base): child1_field = models.TextField() class Child2(Base): child2_field = models.TextField() class Content(models.Model): aso_item...

Django ORM: Getting rows based on max value of a column

Hi, I have a class Marketorders which contains information about single market orders and they are gathered in snapshots of the market (represented by class Snapshot). Each order can appear in more than one snapshot with the latest row of course being the relevant one. class Marketorders(models.Model): id = models.AutoField(primary...

Django ORM - assigning a raw value to DecimalField

EDIT!!! - The casting of the value to a string seems to work fine when I create a new object, but when I try to edit an existing object, it does not allow it. So I have a decimal field in one of my models of Decimal(3,2) When I query up all these objects and try to set this field fieldName = 0.85 OR fieldName = .85 It will throw a ...

Django: How to access originating instance from a RelatedManager?

I would like to access the Foo instance foo within my manager method baz: foo.bar_set.baz() baz would normally take an argument of Foo type: BarManager(models.Manager): def baz(self, foo=None): if foo is None: # assume this call originates from # a RelatedManager and set `foo`. # Otherw...

How to create annotation on filtered data in Django ORM?

I have the following models: class ApiUser(models.Model): apikey = models.CharField(max_length=32, unique=True) class ExtMethodCall(models.Model): apiuser = models.ForeignKey(ApiUser) method = models.CharField(max_length=100) #method name units = models.PositiveIntegerField() #how many units method call cost ...

What is simplest way join __contains and __in?

I am doing tag search function, user could observe a lot of tags, I get it all in one tuple, and now I would like to find all text which include at least one tag from the list. Symbolic: text__contains__in=('asd','dsa') My only idea is do loop e.g.: q = text.objects.all() for t in tag_tuple: q.filter(data__contains=t) EDIT: Now...

Django ORM: dynamic columns from reference model in resultset

Creating an app to track time off accrual. Users have days and days have types like "Vacation" or "Sick" Models: DayType Name UserDay Date DayType (fk to DayType) Value (+ for accrual, - for day taken) Note Total I'm trying to generate the following resultset expanding the daytypes across columns. Is this possible in the ...

How to properly query a ManyToManyField for all the objects in a list (or another ManyToManyField)?

I'm rather stumped about the best way to build a Django query that checks if all the elements of a ManyToMany field (or a list) are present in another ManyToMany field. As an example, I have several Persons, who can have more than one Specialty. There are also Jobs that people can start, but they require one or more Specialties to be el...

Django: get aggregated value of two multipied columns

Hi, I need to get aggregated value of two columns. So first multiple them together and then get theirs sum(). Code below naturallz does not work, it is just for clarification. Is it somehow possible or should I use raw SQL? SomeModel.objects.filter(**something).aggregate(Sum('one_column' * 'another_col')) ...

Django: Query works fine with Sqlite3, don't with others Database Management System

I have quite long query with 'Q()', with Sqlite3 works very well, but with postgresql or mysql I have strange error e.g. for postgresql: invalid input syntax for integer: "(" and for mySQL: Truncated incorrect DOUBLE value: '(' How could I run that query with mysql? Where is my mistake? Here is that query: watchersTuple = self.getWa...

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...

Issue with ManyToMany Relationships not updating inmediatly after save

I'm having issues with ManytoMany Relationships that are not updating in a model when I save it (via the admin) and try to use the new value within a function attached to the post_save signal or within the save_model of the associated AdminModel. I've tried to reload the object within those functions by using the get function with the id...

Django models generic modelling

Say, there is a Page that has many blocks associated with it. And each block needs custom rendering, saving and data. Simplest it is, from the code point of view, to define different classes (hence, models) for each of these models. Simplified as follows: class Page(models.Model): name = models.CharField(max_length=64) class Block...

Filter with field has relationship in Django?

I have these models in Django: class Customer(models.Model): def __unicode__(self): return self.name name = models.CharField(max_length=200) class Sale(models.Model): def __unicode__(self): return "Sale %s (%i)" % (self.type, self.id) customer = models.ForeignKey(Customer) total = models.DecimalField...

Complex Django query over foreign keys

I have two models in the same application. The application is called "News", and it has two classes in its model called "Article" and "Category". class Category(models.Model): name = models.CharField(_("Name"), max_length=100) slug = models.SlugField(_("Slug"), max_length=100, unique=True) class Article(models.Model): categ...

Can I work around this ManyToManyField problem?

Ok, so I posted a question recently regarding an error when adding a ManyToManyField The Models are the ones below class MagicType(models.Model): name = models.CharField(max_length=155) parent = models.ForeignKey('self', null=True, blank=True) class Spell(models.Model): name = models.CharField(max_length=250, db_index=...

Merging duplicates in a list? - Question is more complex than it seems

So I have a huge list of entries in a DB (MySql) I'm using Python and Django in the creation of my web application. This is the base Django model I'm using: class DJ(models.Model): alias = models.CharField(max_length=255) #other fields... In my DB I have now duplicates eg. Above & Beyond, Above and Beyond, Above Beyond, ...

Ranking within Django ORM or SQL?

Hi, so... I have a huge list ranked by various values (eg. scores) So I grab the list ordered by these values: players = Player.objects.order_by('-score', '-karma') I would like to: Grab a player and get the neighbouring players P1 score:123 P2 score:122 YOU! score:110 P3 score:90 P2 score:89 Grab the...

Sorting and grouping objects by Date with the Django ORM

I have an Article model which has a date field. I want to query against all Article objects, and have a datatype returned with each distinct year, and then each distinct month within that. for instance archives = { 2009: {12, [, , ...], 11: [...]}, 2008: {12, [...], 11: [...]}, } is this possible? ...