django-models

Modeling a complex relationship in Django

I'm working on a Web service in Django, and I need to model a very specific, complex relationship which I just can't be able to solve. Imagine three general models, let's call them Site, Category and Item. Each Site contains one or several Categories, but it can relate to them in one of two possible ways: one are "common" categories, wh...

django: select_related with entry_set

Should entry_set be cached with select_related? My DB is still getting calls even after I use select_related. The pertinent sections class Alias(models.Model): achievements = models.ManyToManyField('Achievement', through='Achiever') def points(self) : points = 0 for a in self.achiever_set.all() : po...

What is the best way to add an API to a Django application?

I am adding MetaWeblog API support to a Django CMS, and am not quite sure how to layer the application. I am using django_xmlrpc, which allows me to map to parameterised functions for each request. It is just a case of what level do I hook in calls to the django application from the service functions (AddPage, EditPage etc) For django-...

How to use multiple databases in a django applicaion

Hi There, I need to connect to multiple databases in my django application. I want to make connections to all the databases when ever i start the application and use those connections for all the requests. But I did not understand how to do that (setting these databases connections in global environment). I tried to set database connec...

how to check internal database structure

I am new to django (and have no experience in SQL), so maybe there is no standard way of doing this. So assume I use the band,person,membership example from http://docs.djangoproject.com/en/dev/ref/contrib/admin/#working-with-many-to-many-intermediary-models What if I want to make sure, there is at max one membership bertween a fixed p...

define a django model relationship

Hi, I'm writing an app that have recursive relations like this (pseudocode): class atist: name = charfield (...) class press: pub = foreingkey(artist) class works: work = foreingkey(artist) class img: im = foreingkey(works) I'm was thinking if this is the better approach to solve this problem, or if ...

Resetting databases with having OneToOneField crossing different applications in Django

I'm getting the following error when I run ./manage.py reset app1: Error: Error: app1 couldn't be reset. Possible reasons: * The database isn't running or isn't configured correctly. * At least one of the database tables doesn't exist. * The SQL was invalid. Hint: Look at the output of 'django-admin.py sqlreset app2'. That's the S...

django character set with MySQL weirdness

I'm seeing OperationalError (1267, "Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='") It looks like some of my variables are UTF8 strings 'name': 'p\xc7\x9d\xca\x87\xc9\x9f\xc4\xb1\xc9\xa5s Badge' Is this a configuration issue? If so, how can i solve it? I'd like to handle eve...

How do I find the memory address of a Python / Django model object?

An ordinary object, I can use o.__repr__() to see something like '<__main__.A object at 0x9d78fec>' But, say, a Django User just returns <User:bob> How can I see the actual address of one of these, or compare whether two such model-objects are actually the same object or not? ...

Allow changing of User fields (like email) with django-profiles

Django lets you create a model foreign-keyed to User and define it in settings as the official "profile" model holding additional data for user accounts. django-profiles lets you easily display/create/edit that profile data. But the user's primary email address is part of their main account, not part of their extended profile. Therefore ...

Not null ForeignKey('self')

How can I make a ForeignKey refer back to the object itself? I'm trying : Alias(MyBaseModel): type = models.ForeignKey('self') a = Alias() a.type = a a.save() But then when I run it : (1048, "Column 'type_id' cannot be null") I don't want the type to be null, I want it to contain its own ID. I have tons of objects, but only 1 ...

Django queries, generic content_types

I have a question about django content_types In the example of filtering a QuerySet for a generic content type on http://www.djangoproject.com/documentation/models/generic_relations/ there are the following lines. ctype = ContentType.objects.get_for_model(quartz) TaggedItem.objects.filter(content_type__pk=ctype.id, object_id=quartz.id...

Django : I have a save() which is failing. How can I see the SQL that's generated?

I have a failing model.save() in my Django app. How can I see the SQL that was generated by it? ...

Django : Error thrown by database : relation "_mytable" does not exist (for a content_type) . Lost app-name

In Django, I have a model, let's call it "MyTable" which uses a content_type Foreign Key to refer to, amongst other things, Profile. In most cases (as in my unit-tests) I have no trouble with it, but in a certain circumstance in the view I try to save a Profile object (with Profile's .save() ) and the database throws this exception : r...

Adding attributes into Django Model's Meta class

I'm writing a mixin which will allow my Models to be easily translated into a deep dict of values (kind of like .values(), but traversing relationships). The cleanest place to do the definitions of these seems to be in the models themselves, a la: class Person(models.Model, DeepValues): name = models.CharField(blank=True, max_length...

Django RelatedManager's .create() usage?

I have two models: Play and PlayParticipant, defined (in part) as: class PlayParticipant(models.Model): player = models.ForeignKey('Player') play = models.ForeignKey('Play') note = models.CharField(max_length=100, blank=True) A piece of my code has a play p which has id 8581, and I'd like to add participants to it. I'm try...

python - Problem storing Unicode character to MySQL with Django

I have the string u"Played Mirror's Edge\u2122" Which should be shown as Played Mirror's Edge™ But that is another issue. My problem at hand is that I'm putting it in a model and then trying to save it to a database. AKA: a = models.Achievement(name=u"Played Mirror's Edge\u2122") a.save() And I'm getting : 'ascii' codec can...

Django: Access model as dictionary

I have data in a python dictionary that I'd like to store in a model instance. For example, my dictionary might look like data = { 'date': '6/17/09', 'name': 'something', 'action': 'something' } And my model might look like: class Something(models.Model): date = models.DateField() name = models.CharField() action = models...

Separation of ORM and validation

Hi all, I use django and I wonder in what cases where model validation should go. There are at least two variants: Validate in the model's save method and to raise IntegrityError or another exception if business rules were violated Validate data using forms and built-in clean_* facilities From one point of view, answer is obvious: o...

What is the most efficent way to store a list in the Django models?

Currently I have a lot of python objects in my code similar to the following: class MyClass(): def __init__(self, name, friends): self.myName = name self.myFriends = [str(x) for x in friends] Now I want to turn this into a Django model, where self.myName is a string field, and self.myFriends is a list of strings. from d...