django-models

Django Dump Data For a Single Model?

Can you perform a 'dumpdata' in Django on just a single model (rather than the whole app) and if so, how? E.g. for an app it would be... python manage.py dumpdata myapp However, I want some specific model, (e.g. myapp.mymodel) to be dumped... reason being I have some huge datasets (3 million records plus) in the same app that I would...

Django Model Inheritance And Foreign Keys

Basically, I have a model where I've created a superclass that many other classes share, and then each of those classes has some unique features that differ from each other. Let's say class A is the superclass, and class B, C, and D are children of that class. Both class B and class C can have multiples of class D, however I've seen tha...

Django: How to follow ForeignKey('self') backwards

class Achievement(MyBaseModel): parent_achievement = models.ForeignKey('self', blank=True, null=True, help_text="An achievement that must be done before this one is achieved") # long name since parent is reserved I can do : Achievement.objects.get(pk="1").parent_achievement which is great. But how do I get all the children? Ach...

Django Model Sync Table

If I change a field in a Django model, how can I synchronize it with the database tables? Do I need to do it manually on the database or is there a tool that does helps with the process? ...

Set Django IntegerField by choices=... name

When you have a model field with a choices option you tend to have some magic values associated with human readable names. Is there in Django a convenient way to set these fields by the human readable name instead of the value? Consider this model: class Thing(models.Model): PRIORITIES = ( (0, 'Low'), (1, 'Normal'), (2, '...

How to model one way one-to-one relationship in Django

Hi, I want to model an article with revisions in Django: I have following in my article's models.py: class Article(models.Model): title = models.CharField(blank=False, max_length=80) slug = models.SlugField(max_length=80) def __unicode__(self): return self.title class ArticleRevision(models.Model): article = ...

Django: Converting an entire Model into a single dictionary

Is there a good way in Django to convert an entire model to a dictionary? I mean, like this: class DictModel(models.Model): key = models.CharField(20) value = models.CharField(200) DictModel.objects.all().to_dict() ... with the result being a dictionary with the key/value pairs made up of records in the Model? Has anyone els...

Django : How can I find a list of models that the ORM knows?

In Django, is there a place I can get a list of or look up the models that the ORM knows about? ...

How would you inherit from and override the django model classes to create a listOfStringsField?

I want to create a new type of field for django models that is basically a ListOfStrings. So in your model code you would have the following: models.py: from django.db import models class ListOfStringsField(???): ??? class myDjangoModelClass(): myName = models.CharField(max_length=64) myFriends = ListOfStringsField() # ...

Django: Why don't foreign key lookups automatically use the pk?

I have class Achievement(MyBaseModel): pass class Alias(MyBaseModel): achievements = models.ManyToManyField('Achievement') >>> ach = Achievement.objects.all()[1] This works : >>> Alias.objects.all().filter(achievements__pk__contains=ach.pk).count() 77L But this doesn't : >>> Alias.objects.all().filter(achievements__cont...

What is the best django model field to use to represent a US dollar amount?

I need to store a U.S. $ dollar amount in a field of a Django model. What is the best model field type to use? I need to be able to have the user enter this value (with error checking, only want a number accurate to cents), format it for output to users in different places, and use it to calculate other numbers. ...

Using and Installing Django Custom Field Models

I found a custom field model (JSONField) that I would like to integrate into my Django project. Where do I actually put the JSONField.py file? -- Would it reside in my Django project or would I put it in something like: /django/db/models/fields/ Since I assume it can be done multiple ways, would it then impact how JSONField (or any cu...

Django: Why some fields clashes with other

Hi! I want to create an object that contains 2 links to users. For example class GameClaim(models.Model): target = models.ForeignKey(User) claimer = models.ForeignKey(User) isAccepted = models.BooleanField() but I am getting an error when running server: Accessor for field 'target' clashes with related field 'User.gamecl...

Many choices in one model's field?

I have ` CATEGORY_CHOICES = ( ('A', 'B', 'C') )` and I would let field: myChoice = models.CharField(choices=CATEGORY_CHOICES) have many values from CATEGORY_CHOICES (1-3). I am just starting use Django so an example will be nice :) ...

Signals registered more than once in django1.1 testserver

I've defined a signal handler function in my models.py file. At the bottom of that file, I use signals.post_save.connect(myhandler, sender=myclass) as recommended in the docs at http://docs.djangoproject.com/en/dev/topics/signals/. However, when I run the test server, simple print-statement debugging shows that the models.py file gets i...

Attribute Cache in Django - What's the point?

I was just looking over EveryBlock's source code and I noticed this code in the alerts/models.py code: def _get_user(self): if not hasattr(self, '_user_cache'): from ebpub.accounts.models import User try: self._user_cache = User.objects.get(id=self.user_id) except User.DoesNotExist: ...

map raw sql to django orm.

Is there a way to simplify this working code? This code gets for an object all the different vote types, there are like 20 possible, and counts each type. I prefer not to write raw sql but use the orm. It is a little bit more tricky because I use generic foreign key in the model. def get_object_votes(self, obj): """ Get a dicti...

Django models - self join on both FK in a M2M scenario using QuerySet

Let's take following M2M scenario. I want to get all the colleagues of any given student, and the number of courses both of them attend. (Meaning how many courses the given student has in common with each and any of his colleagues.) class Student(models.Model): pass class Course(models.Model): students = models.ManyToManyField...

How to make email field unique in model User from contrib.auth in Django

I need to patch the standard User model of contrib.auth by ensuring the email field entry is unique: User._meta.fields[4].unique = True Where is best place in code to do that? I want to avoid using the number fields[4]. It's better to user fields['email'], but fields is not dictionary, only list. Another idea may be to open a new ti...

models.py getting huge, what is the best way to break it up?

Directions from my supervisor: "I want to avoid putting any logic in the models.py. From here on out, let's use that as only classes for accessing the database, and keep all logic in external classes that use the models classes, or wrap them." I feel like this is the wrong way to go. I feel that keeping logic out of the models just to ...