django-models

"Best" way to conditionally display the values of different model fields in Django templates?

I'm building a site which will provide product information in two languages: English and Chinese. Each product must have an English name, and may also have a Chinese name. Each time a product page is requested, the request object is inspected to determine whether the product's name should be displayed in English or Chinese. In the latt...

Auto-truncating fields at max_length in Django CharFields

I have a field that has a max_length set. When I save a model instance, and the field's value is greater than max_length, Django enforces that max_length at the database level. (See Django docs on models: http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.CharField.max_length) However, since I am using Postgres, I ...

a fairly complex django query

I've got class Supplier(Model) : pass class Customer(Model) : pass class Dock(Model) : pass class SupplierDockAccess(Model) : supplier = ForeignKey(Supplier) dock = ForeignKey(Dock) class SupplierCustomerAccess(Model): supplier = ForeignKey(Supplier) customer = ForeignKey(Customer) I have an instance of C...

Is there way to make costum signal when Manytomany relations created? Django!

Is there way to make custom signal when ManyToMany relations created? ...

How to implement non-database backed models in Django?

I have an existing Django application with a pretty typical model implementation that's backed by a database. My task is to change this model so that instead of fetching the information from a database, it now fetches it from a service (e.g., via HTTP). Because there is existing code which already makes use of this model, it would be nic...

Check if OneToOneField is None in Django

I have two models like this: class Type1Profile(models.Model): user = models.OneToOneField(User, unique=True) ... class Type2Profile(models.Model): user = models.OneToOneField(User, unique=True) ... I need to do something if the user has Type1 or Type2 profile: if request.user.type1profile != None: # do somethin...

Is there a way to modify the storage backend used by a Django FileField?

I am saving some uploaded files with a Django FileField set to use DefaultStorage backend. At some point after the file has been uploaded I'd like to move them to a different storage backend i.e. change the FileField's storage attribute (obv. after saving the contents of the source file to the new storage location). Simply changing the...

django display content manytomanyfield

Hi all, I'm trying to show the content of a manytomanyfield in the admin interface. I have the following code: class Member(models.Model): group = models.ManyToManyField('Group') def group_gp_name(self): return self.group.gp_name def __unicode__(self): return u'%s' % (self.id) class Group(models.Model): ...

Modify Django user delete method?

If I go to the Django admin page and delete a user, I want it to run some code before/after it deletes the user. I know about overriding models' delete() methods, but I'm not sure how to apply it to a built-in model. Also, I'd like to be able to do it without 'subclassing' the User model and creating a (for instance) MyUser model. Is t...

Is a Django ManyToMany add() an append fuction?

If my models.py has a ManyToMany relationship between books and authors, and if for a particular SampleBook I execute: Sample_book.authors.add(author1) Sample_book.authors.add(author2) Sample_book.authors.add(author3) are author1, author2, and author3 stored in books.authors.all in the order in which they were added? i.e. is the ManyTo...

Resuable model members in django

I have a django model like this: class Something(models.Model): title = models.CharField(max_length=200, default=u'') text = models.CharField(max_length=250, default=u'', blank=True) photo = models.ImageField(upload_to=u'something') def photo_thumb(self): if self.photo: return u'<img src="%s" />'...

How do I set default field value to value of other field in a Django model?

If I have the following model in django; class MyModel(models.Model): name = models.CharField(max_length=50) fullname = models.CharField(max_length=100,default=name) How do I make the fullname field default to name? As it is right now, the fullname defaults to the string representation of the name CharField. Example: new ...

How to serialize to json format a queryset that use the 'extra' statement in Django?

Hi, I want to serialize a QuerySet that contains an extra statement: region_list = Region.objects.extra(select={ 'selected': 'case when id = %s then 1 else 0 end' % (new_region.id)}).all() I use the statement below to serialize return HttpResponse(serializers.serialize('json', region_list), mimetype='application/json') But when I o...

Two classes: one for the django model and one for representing the data outside of django

Is there a reasonable pattern for handling an object that exists as a Django model within the context of a particular Django application and as a non-Django class outside of that particular application? For example, let's say that I have a Post model within Django application Blog. I want to be able to interact with a class representing...

querying a array in django

idarr = [1,2,3,4,5] for i in range(len(idarr)): upload.objects.filter(idarr[i]) Cant we pass the idarr at one shot to the query ...

Django Registration Number of users logged in

I have an application where I would like to display the number of users that are logged into the system. Im trying to determine the cleanest way to do this that will not create a large load on the system. I know that the system keeps track of the "last logged in time" So I could do a query and aggreate the number of users whose last log...

How do I design my models with "interchangeable" fields?

Sorry about the title, I couldn't find the right word. I'll try my best to describe it properly. I have a business model which needs some properties. But the properties of the business depends on what category it is. So for example, I have a business named "XYZ" under the category "Restaurant" and a business named "ABC" under the cate...

Django unique_together doesn't work with ForeignKey=None

I saw some ppl had this problem before me, but on older versions of Django, and I'm running on 1.2.1. I have a model that looks like: class Category(models.Model): objects = CategoryManager() name = models.CharField(max_length=30, blank=False, null=False) parent = models.ForeignKey('self', null=True, blank=True, help_text=_('The di...

Django app to retrieve data from other apps models?

I have an app name sync which has a form created from a model that saves itself. I want to create another app called activity that retrieves the data from the sync models and other future apps. How can I do that in the activity views app? This is my sync models.py from django.db import models from django.contrib.auth.models import User...

Custom Manager to filter objects on site but not in admin?

I followed this example and it works great but I'm wondering if I can put in an exception so that when I am in the admin all objects show up (active and inactive). This might be simple but I can't find how to do it in the docs. Here's what my manager looks like now: class ShareManager(models.Manager): def get_query_set(self): ...