django-models

Resize fields in Django Admin

Django tends to fill up horizontal space when adding or editing entries on the admin, but, in some cases, is a real waste of space, when, i.e., editing a date field, 8 characters wide, or a CharField, also 6 or 8 chars wide, and then the edit box goes up to 15 or 20 chars. How can I tell the admin how wide a textbox should be, or the he...

Django objects change model field

This doesn't work: >>> pa = Person.objects.all() >>> pa[2].nickname u'arst' >>> pa[2].nickname = 'something else' >>> pa[2].save() >>> pa[2].nickname u'arst' But it works if you take p = Person.objects.get(pk=2) and change the nick. Why so. ...

Django Contenttypes and decorator

Hi. The site makes use of 2 objects - articles and blogs. Every time an article or blog is viewed, a related counter should increase by one. The idea is to have a "top ten" application that measures the "popularity" of the articles and entries. Because I'm using more than one object, I would like the Tracker model to use a genericFo...

Admin Form Validation

I've got a Supplier Invoice (SupplierInvoice) parent model that holds a number of orders (SupplierOrder). Right now if the user puts together an invoice via django admin, django checks to see if there are price matches for that Supplier and Product in a cost price table and pulls through the respective fields. This process happens on a...

view on site

I am using def get_absolute_url in my model. It is giving the wrong "View on Site" link. How can it be corrected? ...

template django

how can i use different template in different application.in a project i have two app 1)Site 2)Ad .I want to use default template in Ad but different in Site..How to ?OR in the template is there is a way to use 'if condition' as i have to change only two lines in the templates. ...

Hierarchical Data Models: Adjacency List vs. Nested Sets

I have a product catalog. Each category consists of different number (in deep) of subcategories. The number of levels (deep) is unknown, but I quite sure that it will not be exceed of 5,6 levels. The data changes are much more rarely then reads. The question is: what type of hierarchical data model is more suitable for such situation. T...

Django Model Inheritance: Duplicated class fields

I have a Django project whereby every model inherits from a common "Object" model - which defines only two fields - the ID of the object (so every object in the entire system has a unique identifier) and a "type". The type is the type of object that particular instance is. This is a sort of "denormalized" field, making it faster to trave...

Are asynchronous Django model queries possible?

I'm new to Django, but the application that I have in mind might end up having URLs that look like this: http://mysite/compare/id_1/id_2 Where "id_1" and "id_2" are identifiers of two distinct Model objects. In the handler for "compare" I'd like to asynchronously, and in parallel, query and retrieve objects id_1 and id_2. Is there a...

django model with two generic (content_type) foreign keys?

I'm trying to create a mapping table between two generic (content_type) references, one for "agents" and one for "resources". So I take the usual way I make a generic foreign key : content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() resource = generic.GenericForeignKey('content_type', 'object_id') ...

does custom user class break applications in django?

Let's say that I have subclassed User model (CustomUser) properly (as explained here: http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/) and installed the comments app. to access the user of a comment in the template I write: {{comment.user}} # which provides User, not my CustomUser and theref...

how to override the verbose name of a superclass model field in django

Let's say that I have a model Foo that inherits from SuperFoo: class SuperFoo(models.Model): name = models.CharField('name of SuperFoo instance', max_length=50) ... class Foo(SuperFoo): ... # do something that changes verbose_name of name field of SuperFoo In class Foo, I'd like to override the verbose_name of the name fi...

Django ORM Query to limit for the specific key instance.

Projectfundingdetail has a foreign key to project. The following query gives me the list of all projects that have any projectfundingdetail under 1000. How do I limit it to latest projectfundingdetail only. projects_list.filter(projectfundingdetail__budget__lte=1000).distinct() I have defined the following function, def latest_fundi...

how to sort by a computed value in django

Hey I want to sort objects based on a computed value in django... how do I do it? Here is an example User profile model based on stack overflow that explains my predicament: class Profile(models.Model): user = models.ForeignKey(User) def get_reputation(): ... return reputation reputation = property(get_rep...

Generic many-to-many relationships

I'm trying to create a messaging system where a message's sender and recipients can be generic entities. This seems fine for the sender, where there is only object to reference (GenericForeignKey) but I can't figure out how to go about this for the recipients (GenericManyToManyKey ??) Below is a simplified example. PersonClient and Comp...

Django-like abstract database API for non-Django projects

I love the abstract database API that comes with Django, I was wondering if I could use this (or something similar) to model, access, and manage my (postgres) database for my non-Django Python projects. ...

Django: Order a model by a many-to-many field

I am writing a Django application that has a model for People, and I have hit a snag. I am assigning Role objects to people using a Many-To-Many relationship - where Roles have a name and a weight. I wish to order my list of people by their heaviest role's weight. If I do People.objects.order_by('-roles__weight'), then I get duplicates w...

Django : Setting a generic (content_type) field with a real object sets it to None

Update 3 (Read This First) : Yes, this was caused by the object "profile" not having been saved. For those getting the same symptoms, the moral is "If a ForeignKey field seems to be getting set to None when you assign a real object to it, it's probably because that other objects hasn't been saved." Even if you are 100% sure that it wa...

Keep code from running during syncdb

hello again! I have some code that throws causes syncdb to throw an error (because it tries to access the model before the tables are created). Is there a way to keep the code from running on syncdb? something like: if not syncdb: run_some_code() Thanks :) edit: PS - I thought about using the post_init signal... for the code th...

django admin foreignkey default value

How can I set a default value on a ForeignKey field in a django Model or AdminModel? Something like this (but of course this doesn't work)... created_by = models.ForeignKey(User, default=request.user) I know I can 'trick' it in the view, but in terms of the AdminModel it doesn't seem possible. ...