django-models

Saving ForeignKey objects in Django

I'm completely stumped as to why this isn't working: flight = Flight.objects.get(pk=flight_id) print "old", flight.route.pk ## `route` is a ForeignKey field to model Route print "new", new_route.pk flight.route=new_route # new_route is a newly created Route object flight.save() print "db", Flight.objects.get(pk=flight_id).route.pk th...

How to save a model without sending a signal?

How can I save a model, such that signals arent sent. (post_save and pre_save) ...

In Django, How Do I Move Images When A Dynamic Path Changes?

I have a Django app with an image field (a custom ThumbnailImageField type) that auto-generates the file path for an image based on the title, type, and country of the item the image is attached to (upload_ to = get_ image_path). Here's how: def get_image_path(instance, filename): dir = 'images' subdir = instance.get_type_disp...

How to organize Django project with abstract models.

I have a few models: 'Article, Video, BlogPost, News, Commodity'. Each are in their own application. They all are basically the same models with a few extra fields on each. But each share about 15 fields. I'm using an abstract base class. I'm trying to figure out how I should do organization for this. My current setup is like this: app...

Custom form in inline form

I have a custom form to display goals. Goals are edited inline in a Game. class GoalForm(forms.ModelForm): class Meta: model = Goal def __init__(self, *args, **kwargs): super(GoalForm, self).__init__(*args, **kwargs) self.fields['goal_scorer'].queryset = Player.objects.filter(gameroster__game=self.instance.g...

Can a dictionary be passed to django models on create?

Is is possible to do something similar to this with a list, dictionary or something else even? data_dict = { 'title' : 'awesome title', 'body' : 'great body of text', } Model.objects.create(data_dict) Even better if I can extend it Model.objects.create(data_dict, extra='hello', extra2='world) ...

Can I use Django's Generic Views with google-app-engine-django?

Put simply, is there a way to get generic views to work? If I try the following in urls.py: publisher_info = { 'queryset': Publisher.objects.all(), } urlpatterns = patterns('', (r'^publishers/$', list_detail.object_list, publisher_info) ) I get the following error: AttributeError at /publishers 'Query' object has no a...

How to update multiple fields of a django model instance?

I'm wondering, what is a standard way of updating multiple fields of an instance of a model in django? ... If I have a model with some fields, Class foomodel(models.Model): field1 = models.CharField(max_length=10) field2 = models.CharField(max_length=10) field3 = models.CharField(max_length=10) ... ... and I instantiat...

Django inheritance: how to have one method for all subclasses?

I have a model BaseModel and several subclasses of it ChildModelA(BaseModel), ChildModelB(BaseModel), ... using multi-table inheritance. In future I plan to have dozens of subclass models. All subclasses have some implementation of method do_something() How can I call do_somthing from a BaseModel instance? Almost identica...

Storing wiki revisions on Google App Engine/Django - Modifying This Existing Code

In the past, I created a Django wiki, and it was fairly straightforward to make a Page table for the current wiki entries, and then to store old revisions into a Revision table. More recently, I decided to set up a website on Google App Engine, and I used some wiki code that another programmer wrote. Because he created his Page model in...

django and executing a separate .py to manipute a database

Hello, I want to execute a random .py file, say foo.py on the myproject/myapp folder by using crobjob by some periods I have this basic model in my model.py for the app: class Mymodel(models.Model): content = models.TextField() Say I have this in my foo.py, I want to check if there is any Mymodel object that has a content field a...

Complex query in Django

Maybe a simple question, but i've just started to work with Django after a few years of php experience :-) Problem: we have a pair of models - "Categories" and "Post". "Categories" is nested sets tree of post categories, "Post" is plain list of blog posts with ForeignKey field, linked to Categories model. Here is an example: class Cate...

How do you model this in django?

Considering the class model as follows: How do you do this in models.py? class House(models.Model): foo = models.CharField(max_length=123) class Caravan(models.Model): foo = models.CharField(max_length=123) class Door(models.Model): bar = models.CharField(max_length=123) house = models.ForeignKey(House) caravan ...

django syncdb and an updated model

Hello, I have recently updated my model, added a BooleanField to it however when I do python manage.py syncdb, it doesn't add the new field to the database for the model. How can I fix this ? ...

DRY: Only show records owned by user. Possible with models.Manager?

Hi all, I need to show a user only the objects that he owns. Since I need to do this on more then 80% of my views, hardcoding this kills DRY. More so, it is absolutely imperative that a user never sees records owned by others. And doing it by hand (in all the views) also seems error prone. I've been looking at decorators (like login_re...

Must Django ManyToManyField association tables have a surrogate key?

I'm mapping an existing database structure into Django models. I have a many-to-many structure where the association table is natural-keyed: CREATE TABLE foo (id INTEGER PRIMARY KEY); CREATE TABLE bar (id INTEGER PRIMARY KEY); CREATE TABLE foo2bar (foo_id INTEGER REFERENCES foo(id), bar_id INTEGER REFERENCES bar(id...

Abstract base class inheritance in Django with foreignkey

I am attempting model inheritance on my Django powered site in order to adhere to DRY. My goal is to use an abstract base class called BasicCompany to supply the common info for three child classes: Butcher, Baker, CandlestickMaker (they are located in their own apps under their respective names). Each of the child classes has a need fo...

How to query a Django model defining a IP range with two int fields (IP, mask)

I have: class Range(models.Model): ip = models.IntegerField() # as produced by socket.inet_aton + struct.unpack mask = models.IntegerField() Given a certain IP, how can I get all the ranges that match this specific IP using the Django models? If I were using raw SQL, I would use the database's bitwise operators, but the Djang...

Define an attribute in a model, like an object of the other model in Django

Is posible to define an attribute in a data model like an object of other data model in Django? This is the scenary: models.py class Inmueble(models.Model): calle = models.CharField(max_length=20, verbose_name="Calle") numero = models.CharField(max_length=6, verbose_name="Numero") piso = models.IntegerField(verbose_name="Pi...

Django or similar for composite primary keys

I am writing a web application for my engineering company (warning: I am a programmer only by hobby) and was planning on using Django until I hit this snag. The models I want to use naturally have multi-column primary keys. Per http://code.djangoproject.com/ticket/373, I can't use Django, at least not a released version. Can anyone help ...