django-models

Django model class and custom property

Howdy - today a weird problem occured to me: I have a modle class in Django and added a custom property to it that shall not be saved into the database and therefore is not represent in the models structure: class Category(models.Model): groups = models.ManyToManyField(Group) title = defaultdict() Now, when I'm within the she...

How can I filter these Django records?

I have a set of Django models as shown in the following diagram (the names of the reverse relationships are shown in the yellow bubbles): In each relationship, a Person may have 0 or more of the items. Additionally, the slug field is (unfortunately) not unique; multiple Person records may have the same slug fields. Essentially these ...

Django models avoid duplicates

In models, class Getdata(models.Model): title = models.CharField(max_length=255) state = models.CharField(max_length=2, choices=STATE, default="0") name = models.ForeignKey(School) created_by = models.ForeignKey(profile) def __unicode__(self): return self.id() In templates <form> <input type="submit" s...

SQL commands generated in Django by running sqlall

In my Django app, I just ran $ python manage.py sqlall and I see a lot of SQL statements that look like this, when describing FK relationships: ALTER TABLE `app1_model1` ADD CONSTRAINT model2_id_refs_id_728de91f FOREIGN KEY (`model2_id`) REFERENCES `app1_model2` (`id`); Where does "7218de91f" come from? I would like to know becau...

Can django lazy-load fields in a model?

One of my django models has a large TextField which I often don't need to use. Is there a way to tell django to "lazy-load" this field? i.e. not to bother pulling it from the database unless I explicitly ask for it. I'm wasting a lot of memory and bandwidth pulling this TextField into python every time I refer to these objects. The al...

Does a Postgresql dump create sequences that start with - or after - the last key?

I recently created a SQL dump of a database behind a Django project, and after cleaning the SQL up a little bit was able to restore the DB and all of the data. The problem was the sequences were all mucked up. I tried adding a new user and generated the Python error IntegrityError: duplicate key violates unique constraint. Naturally I ...

Automark model names/attributes for translation

Is there any way one could automatically mark all model names and attributes for translation, without specifying verbose_name/_plural on each one of them? Doesn't feel very DRY to do this every time: class Profile(models.Model): length = models.IntegerField(_('length')) weight = models.IntegerField(_('weight')) favorite_mov...

Reverse relationships and distinct querysets.

I have the following models and I'm trying to work out how to do backward relationships. I want a distinct CostRate queryset showing which costrates are associated with a particular SalesEvent. The CostFixedList has all the sales that occurred on the different days. So I'd filter the CostFixedList for a particular SalesEvent, create ...

Complex relationships with filtering

I'm stuggling to get to grips with relationships in ORM. I want to get a distinct CostItem queryset related to a particular event. Normally I'd filter CostFixedList for the particular event I'm interested and get a the Cost_Rate id's. From that I could then get the CostItem id's. I could do this easily in SQL, but can't understand ...

Django: How can I add weight/ordering to a many to many relationship?

I'm having Pages with TextBlocks on them. Text Blocks may appear on different pages, pages may have several text blocks on them. Every page may have these blocks in an ordering of it's own. This can be solved by using a separate through parameter. Like so: class TextBlock(models.Model): title = models.CharField(max_length=255) ...

Django - Can you use property as the field in an aggregation function?

I know the short answer because I tried it. Is there any way to accomplish this though (even if only on account of a hack)? class Ticket(models.Model): account = modelfields.AccountField() uuid = models.CharField(max_length=36, unique=True) created = models.DateTimeField(auto_now_add=True) class Meta: ordering =...

How to store an integer leaded by zeros in django

Hello, I'm trying to store a number in django that looks like this: 000001 My problem is that if I type this inside an IntegerField it gets converted to "1" without the leading zeros. I've tried also with a DecimalField with the same result. How can I store the leading zeros whithout using a CharField? (I need to manipulate that numb...

Accessing updated M2M fields in overriden save() in django's admin

I'd like to use the user updated values of a ManyToManyField in a model's overriden save() method when I save an instance in admin. It turns out that by design, django does not update the M2M field before calling save(), but only after the save() is complete as part of the form save... e.g. in both print commands bellow the values disp...

Django QuerySet next date

My model has a DateField. If I wish to retrive all data that have a date after today, how will it be constructed? Eg. retreive all field with dates after today and stop at 31st december today = datetime.date.today Object.object.filter(date__day != today) or will it be done using exclude? ...

Custom properties in a query

Given the simplified example below, how would I access my custom "current_status" property within a queryset? Is it even possible? At the moment, I want to list the all the current Events and show the current status. I can get the property to display in a template ok, but I can't order the queryset by it. Alternatively, would I need to ...

IntegrityError: (1062, "Duplicate entry '1830327-1792993' for key 'some_instance_A_id'") but no UNIQUE constraint

I'm rarely getting such exception when adding model instance to many2many field, i.e.: some_instance_A.my_m2m.add(some_instance_B)). It works say 99/100 times. What looks strange to me, is that dash sign - as primary keys are integers.. Model field is defined like this: my_m2m = ManyToManyField(B) so it's the simplest M2M defi...

Django: Many-to-many through a table with (only) compound key

I have a legacy database with a table storing a many-to-many relationship, but without a single primary key column. Is there any way to convince Django to use it anyway? Schematically: Product 1<---->* Labeling *<---->1 Label The Labeling table uses (product_id,label_id) as a compound primary key, and I don't see any way to inform Dj...

Django: Call self function inside a Django model

I want to call for a self function of a model class as such in upload_to: class Foo(models.Model): filestack = models.FileField(upload_to=self. gen_save_path) def gen_save_path(self): """ gen_save_path: void -> String Generates the path as a string for fileStack field. """ return "some ge...

How can I get the Django Queryset cache to reset?

I manually changed the contents of a record in the database. The changes are not reflected in the app. I'm going to reset it by using the shell to load the query and then resave it. Is there another, easier way? ...

set user session in django

<?php session_start(); $_SESSION['username'] = "johndoe" // Must be already set ?> How to write equivalent code for the above in django ...