django-models

Django model fields validation

Where should the validation of model fields go in django? I could name at least two possible choices: in the overloaded .save() method of the model or in the .to_python() method of the models.Field subclass (obviously for that to work you must write custom fields). Possible use cases: when it is absolutely neccessary to ensure, that ...

Is there an easy way to create derived attributes in Django Model/Python classes?

Every Django model has a default primary-key id created automatically. I want the model objects to have another attribute big_id which is calculated as: big_id = id * SOME_CONSTANT I want to access big_id as model_obj.big_id without the corresponding database table having a column called big_id. Is this possible? ...

chaining queries together in Django.

I have a query that gets me 32 avatar images from my avatar application: newUserAv = Avatar.objects.filter(valid=True)[:32] I'd like to combine this with a query to django's Auth user model, so I can get the last the last 32 people, who have avatar images, sorted by the date joined. What is the best way to chain these two together? ...

In django, how to write a query that selects all possible combinations of four integers?

I'm writing a Game Website, where the draw is a series of four digits. e.g 1234 I"m trying to write a query in django that will select all winners based on the four digits entered. winners are any combination of the same numbers or the same combination, 1 2 3 4, 2 3 1 4, 4 1 3 2 are all winners. how is the most efficient way to write ...

How to access more databases in Django using SQLite 3

In Django I am using two applications: python manage.py startapp books python manage.py startapp contacts Still I am only using the books application models. So I am using DATABASE_NAME like: DATABASE_NAME = 'C:/WorkBase/Python/first/books/book.db' Now I want to use the second one contacts application models. How can I add cont...

django model objects to a python list

hello I want to generate a list using my django model say I have these model: class AlarmServer(models.Model): ip = models.IPAddressField() and such a list server_ips = [i.ipfor i in AlarmServer.objects.all()] Doesn't seem to work, what am I doing wrong? ...

django queryset excluding entries in second model

Hello, I'm making a little vocabulary-quiz app, and the basic model for a word is this: class Word(models.Model): id = models.AutoField(primary_key=True) word = models.CharField(max_length=80) id_image = models.ForeignKey(Image) def __unicode__(self): return self.word class Meta: db_table = u'word' ...

How to obtain a count of objects per auth.user?

I have a Project model similar to: class Project(models.Model): ... lead_analyst=models.ForeignKey(User, related_name='lead_analyst') ... I want to use django aggregation to return all users with a count of projects per user. Something like: models.User.objects.annotate(project_count=Count('project_set')) Only that does...

post_save in django to update instance immediately

hello, I'm trying to immediately update a record after it's saved. This example may seem pointless but imagine we need to use an API after the data is saved to get some extra info and update the record: def my_handler(sender, instance=False, **kwargs): t = Test.objects.filter(id=instance.id) t.blah = 'hello' t.save() class...

How to store arbitrary name/value key pairs in a Django model?

I have a fixed data model that has a lot of data fields. class Widget(Models.model): widget_owner = models.ForeignKey(auth.User) val1 = models.CharField() val2 = models.CharField() ... val568 = ... I want to cram even more data into this Widget by letting my users specify custom data fields. What's a sane way to ...

Django ORM: Optimizing queries involving many-to-many relations

I have the following model structure: class Container(models.Model): pass class Generic(models.Model): name = models.CharacterField(unique=True) cont = models.ManyToManyField(Container, null=True) # It is possible to have a Generic object not associated with any container, # thats why null=True class Specific1(Gen...

django queryset: how to order by a (backwards) related field?

Hi folks! In this situation I have two models, Comment and Score. The relationship is defined in the Score model, like so: class Comment(models.Model): content = TextField() ... class Score(models.Model): comment = models.ForeignKey(Comment) value = models.IntegerField() My question is: How do I construct a queryset ...

Adapt an existing database to a django app

I have a Postgresql databese with data. I want to create a django app with that database. How can i import the tables to django models and/or views? ...

Correct way to access related objects

Hi, I have the following models class Person(models.Model): name = models.CharField(max_length=100) class Employee(Person): job = model.Charfield(max_length=200) class PhoneNumber(models.Model): person = models.ForeignKey(Person) How do I access the PhoneNumbers associated with an employee if I have the employee id? Cu...

How can I store many Django model types in a database and access them as an object?

I have the following Django models: Bar, Bistro and Restaurant Each of the above establishment has their own menu items, for instance: Bar Burger Fries ... Bistro Pasta Pizza ... Restaurant Duck Wings ... I have different type of images in my home page, a main banner, left side bar and right side bar. Each of those images w...

Can Django automatically create a related one-to-one model?

I have two models in different apps: modelA and modelB. They have a one-to-one relationship. Is there a way django can automatically create and save ModelB when modelA is saved? class ModelA(models.Model): name = models.CharField(max_length=30) class ModelB(models.Model): thing = models.OneToOneField(ModelA, primary_key=True)...

Figuring out a scheduler relational model

Hi People. I'm trying to solve the relational model in order to make a Django app. I't will be something like a McDonald's crew scheduler. I mean the grid with colored pins marking who will be working at a given hour a given weekday. I did try to Google out some example, but I didn't find anything. I need some theory/bibliography in o...

How to prepopulate ID in Django

Hello, I have simple question model : class Question(Polymorph): text = models.CharField(max_length=256) poll = models.ForeignKey(Poll) index = models.IntegerField() And I would like to prepopulate ( when saving ) index field with ID value. Of course before save I dont have ID value ( its created after it ), so I wonder w...

YUI.io (ajax) and Django - update works only once, YUI can't find form again

I am using YUI 3.0 io to submit form data with ajax. The form fields are created from a django view (ModelForm). The first time the form loads, I update a field and submit the form using YUI onclick which calls the io callback which runs the django view and returns the ModelForm again with the update data. This works once (saves to datab...

Django models - many classes vs few classes? Working with "not-exactly-inheritance" relationships

I have a rather generic model, as follows: class Keyword(models.Model): ancestors = models.ManyToManyField(Keyword) name = models.CharField() description = models.CharField() Trouble is, I have several different types of keywords, which all have different forms of business logic. Some, for example, are never allowed to hav...