django-models

Help on my django models for my shoe review website.

What i'm trying to achieve at this point: Allow me to add new shoe objects, and shoe review objects. complete Allow "Owner Reviews" on each shoe complete (I think, but there might be somewhere in my model I can improve) Allow ratings of attributes defined by me which are linked to a particular shoe. Preferably, having an easy way to it...

Deep JSON Serialization of Django objects

Consider the following Django Model: class Event(models.Model): startDate = models.DateField() endDate = models.DateField() user = models.ForeignKey(User, null=True) Later in my view I do this: django.core.serializers.serialize("json", Event.objects.all()) return HttpResponse(data, mimetype='application/javascript') ...

Django - same abstract base model for all models? Good, bad?

If I'm building an application that will have over 30 models, and I want the option to plug in a custom manager or other functionality into all the models down the road, is it a good idea to use an abstract base model and subclass it with every model, or is there compelling reasons not to do this? ...

Django admin list filter

I want to add a custom model method to the admin filter, however it fails. Example Foo: class Foo(models.Model): number = models.IntegerField() def big_enough(self): return self.number > 99 now at the admin panel: class FooAdmin(admin.ModelAdmin): list_filter = ('number', 'big_enough') Fails, I get the error...

Get SQL a Django model has (or would call) on .save()

How can you get the SQL for a Django model's .save(), i.e. from django.db import models class MyM(models.Model): text = models.TextField() How can you get the SQL that would be created/used in the following scenario: >>> m = MyM(text="123") >>> m.save() # What SQL Django just run? Thanks! ...

Do unit tests on the 'live' database in settings.py while using Django's 'manage.py test'

If you've got a database setup in Django, how can you have the TestRunner use the 'live' database (per the DATABASE_* settings in settings.py) instead of running them on the ephemeral test database. For example, I'd like to run the following test on the live database that's specified in settings.py: import unittest from example import...

Django multi tennant architecture - Should all models have a reference to the tennant_id

Let's say that accounts in my SAAS are of the type Account(models.Model). Would the following be a good idea? class MyModel(models.Model): account = models.ForeignKey(Account) spam = models.CharField(max_length=255) class MyOtherModel(models.Model): # The next attribute `account` is the line in question. # Should it be ...

Django, relational querysets.

How do I express this SQL query in a Django Queryset? SELECT * FROM Table1, Table2 WHERE Table1.id_table2 = Table2.id_table2; Be aware that the structure of table1 implyes a id_table2 foreign key... Why? Because I want to replace the id_table2 in the Table1 table1.object.all() listing with values asociated to the register involved in...

How to introspect django model fields?

Hello, I am trying to obtain class information on a field inside a model, when I only know name of the field and name of the model (both plain strings). How is it possible? I can load the model dynamically: from django.db import models model = models.get_model('myapp','mymodel') Now I have field - 'myfield' - how can I get the class ...

Django: Overriding the save() method: how do I call the delete() method of a child class

The setup = I have this class, Transcript: class Transcript(models.Model): body = models.TextField('Body') doPagination = models.BooleanField('Paginate') numPages = models.PositiveIntegerField('Number of Pages') and this class, TranscriptPages(models.Model): class TranscriptPages(models.Model): transcript = mode...

Django ORM without HTTP and commit_on_success decorator

Hello I'm trying to use Django's ORM in my non-HTTP part of project. In one function I need to make bulk inserts of data, so commit_on_success decorator is what I need. But I've found that this decorator doesn't do what supposed (didn't rollback transaction on error). I've go into this decorator with debugger, I've fount thar reason...

Difference in Django object creation call

I'd like to know if there's a difference between the following two calls to create an object in Django Animal.objects.create(name="cat", sound="meow") and Animal(name="cat", sound="meow") I see both in test cases and I want to make sure I am not missing something. thanks ...

Set model field choices attribute at run time?

Is there any easy way to do so? ...

can't override default admin model form django

Hello, I need to add extra validation to my DateField in Admin to make sure the date given is in the future. I have no experience in such a thing, so here's what I've done. 1) I've created custom form field and added validation to it: class PastDateField(forms.DateField): def clean(self, value): """Validates if only date is in t...

How to handle non-normalised data

I'm building an database to manage paying some of our freelance sellers. They get a daily rate, commission per product, and other expenses (such as mileage for example). Some of the items and expenses have standard rates. For example, £20 for selling a certain type of item. The will of course change over time. So, for example, this year...

Sorting&grouping QuerySet data in Django

I have BlogEntry model, which has two special fields : "publication_date"(timestamp), "sticky"(boolean). i need to get them in order by "publication_date", and group by "sticky" field that have to be first in query set. what i try to get: [ <BlogEntry:"05.03.2010 sticky:1">, <BlogEntry:"04.03.2010 sticky:1">, <BlogEntry:"03.03.2010 st...

Django Admin: Detect if a subset of an object fields has changed and which of them

I need to detect when some of the fields of certain model have changed in the admin, to later send notifications depending on which fields changed and previous/current values of those fields. I tried using a ModelForm and overriding the save() method, but the form's self.cleaned_data and seld.instance already have the new values of the ...

Django many-to-many relationship to self with extra data, how do I select from a certain direction?

I have some hierarchical data where each Set can have many members and can belong to more than one Set(group) Here are the models: class Set(models.Model): ... groups = models.ManyToManyField('self', through='Membership', symmetrical=False) members = models.ManyToManyField('self', through='Membership', symmetrical=False) c...

Filtering manager for django model, customized by user

Hi there! I have a model, smth like this: class Action(models.Model): def can_be_applied(self, user): #whatever return True and I want to override its default Manager. But I don't know how to pass the current user variable to the manager, so I have to do smth like this: [act for act in Action.objects.all() if a...

AES Encrypting a password field in django using a snippet from djangosnippets

I am attempting to use this snippet: http://www.djangosnippets.org/snippets/1095/ on a model I am building- I am defining things as: first = models.TextField() last = models.TextField() email = models.EmailField(default=None) screen = models.TextField() password = models.EncryptedCharField() icon = models.ImageField(upload_to='avatars...