django-models

Django multi-model: tracing relationships

I have a multi model with different OneToOne relationships from various models to a single parent. Consider this example: class Place(models.Model): name = models.CharField(max_length=100) address = models.CharField(max_length=100) ... class Restaurant(models.Model): place = models.OneToOneField(Place) ... class Sh...

right way to catch a race condition with Django ORM + MySQL/InnoDB

One part of my application has a race condition where multiple threads could end up creating the same persistent object. So I have implemented code that looks like this: from foobar.models import Bar def testomatic(request): bar = None tries = 0 while not bar: try: bar = Bar.objects.get(b=2) ex...

Strange issue when trying to set a BooleanField value in a django model

I'm trying to change the value of a BooleanField in one of my models, but Django won't let me. Here's the relevant code: query = MyModel.objects.filter(name='example').filter(boolField=False) print query[0].boolField query[0].boolField = True query[0].save() print query[0].boolField This surprisingly prints: False False Any idea wh...

Get related (via ForeignKey) object in my view

I have a model like this: class database(models.Model): db_name = models.CharField('Name', max_length=20) server = models.ForeignKey(dbServer, unique=True) user = models.ForeignKey(User) In my view I want to grab every database realated to the current user (has to be logged in at that point). I'm sure there is an easy way to do ...

How to change SQLite DB path for each request in Django?

I would like to dynamically change path to sqlite database depending on currently logged user using Django framework. Basically each user should have his own sqlite database file. Anyone with experience? Thanks. ...

In Django, how to properly access through a proxy model to related proxy models

Hello, I wanted to override the get_absolute_url method in the Django User and Group models from the auth app. My first idea was to define a proxy model, but then I noticed that the elements in usuario.groups were instances of Group instead of Grupo and it also happened the same in the grupo.user_set case. So I expanded the proxy models...

Displaying properties in Django admin - translating their names

In my Django application my model has some values set as properties - they are calculated on demand from other values (like, min. value of some other objects' field, etc). This works pretty well as I don't need to store those in the database and the calculations can be expensive, so they're cached. So I have a model: class A(models.Mo...

Is it possible to join model types in Django into one object?

For example I have two model objects, Person and Address. Address has a reference to a Person id. What would a query look like that pulls them out together as one object, or is that not possible to do with Django? ...

In Django I have a complex query where I need only the unique values through a foreign key, is this possible?

I have the following models: class Indicator(models.Model): name = models.CharField(max_length=200) category = models.ForeignKey(IndicatorCategory) weight = models.IntegerField() industry = models.ForeignKey(Industry) def __unicode__(self): return self.name class Meta: ordering = ('name',) clas...

in django how do I create a queryset to find double barrel names?

In django, I have a table of people, each of which has a namefirst and namelast. I want to do the sql: select * from names where left(namefirst,1)=left(namelast,1). Right now my best effort is qs=People.objects.extra(select={'db':'select left(namefirst,1)=left(namelast,1)'}) but then if i stick a .filter(db=1) on that it genera...

Huge table in a Django ?

hi, if you have millions of entries in a mysql table, you often want to manipulate it with a hexadecimal primary key (in practice you do a md5(name)). The queries are then much faster. Is there a way to do this with Django? If not, the usual int primary key isn't limitating? How to specify that you want a big integer? ...

modelform fails is_valid w/o setting form.errors

I'm using a modelform to display only a subset of the fields in the model. When the form is submitted it fails form.is_valid() but form.errors is empty. I'd rather not display all my code here, but below is a sample: Model and Form class Videofiles(models.Model): active = models.CharField(max_length=9) filenamebase = models.Cha...

Django: How to find out whether RawQuetySet has rows ? There's no count() method

Hello, I think the title speaks for itself. I have a complex query with a subquery, but sometimes it returns no values, which is absolutely normal. But I can not prevent the ValueError message, cuz I am not able to find out whether RawQuerySet is empty or not. The RQS object is always present, but if I try to access it's first row resul...

Django: limiting model data

Hi there! I'm searching in a way to limit the queryset which I can get through a model. Suppose I have the following models (with dependencies): Company |- Section | |- Employee | |- Task | `- more models... |- Customer | |- Contract | |- Accounts | `- other great models ... `- some more models... It should be n...

usage of class inheritance in django for polymorphism?

Dear friends, I need your help in the following matter : in my django models, the following classes exist : class QItem(models.Model) class Question(QItem) class QuestionSet(QItem): items = models.ManyToManyField(QItem, blank=True, null=True, through='Ordering', related_name="contents") class Ordering(models.Model): item ...

How can I track a user events in Django?

I'm building a small social site, and I want to implement an activity stream in a users profile to display event's like commenting, joining a group, posting something, and so on. Basically, I'm trying to make something similar to a reddit profile that shows a range of user activity. I'm not sure how I'd do this in Django though. I thoug...

Formset Creating New Entries Instead of Updating

I have the following code in a view: def controller_details(request, object_id): controller = Controller.objects.get(pk=object_id) controllerURI = controller.protocol + '://' + controller.server + '/' + controller.name FilterFormSet = inlineformset_factory(Controller, Filter, extra=2) if request.method == 'POST': ...

Remove all the elements in a foreign key select field

I have a foreign key reference which shows up as a select box on the client but it is pre-populated with values. I want the select box to be empty when it shows because it will be populated by an Ajax call. This is my model class RecipeIngredient(models.Model): recipe = models.ForeignKey(Recipe) ingredient = models.ForeignKey(I...

Django: in a 1 to N model, is there a simple way to look up related objects?

class Blog(models.Model): name = models.CharField() def get_posts_belonging_to_this_blog_instance(self): return Post.objects.filter(blog__exact=self.id) class Category(models.Model): name = models.CharField() def get_posts_with_this_category(self): return Post.objects.filter(category__exact=self.id) class Post(models.Model): ...

How many rows were deleted?

Is it possible to check how many rows were deleted by a query? queryset = MyModel.object.filter(foo=bar) queryset.delete() deleted = ... Or should I use transactions for that? @transaction.commit_on_success def delete_some_rows(): queryset = MyModel.object.filter(foo=bar) deleted = queryset.count() queryset.delete() PHP...