django-models

[Django] Insert AUTOMATICALLY random string in username field

I want username field is automatically filled with this value: username = str(n); where n is a number ( autoincremented or random ). . I tried to add this in save method: username = str(random.randint(0,1000000) but there is a collision problem when n is the same for 2 users. . How do I do this ? Thanks ^_^ ...

django order by related field

Hi, I want to sort a QuerySet of contacts by a related field. But I do not know how. I tried it like this, but it does not work. foundContacts.order_by("classification.kam") Actually in a template I can access the kam value of a contact through contact.classification.kam since it is a OneToOne relationship. The (simplified) models l...

How do I define an ImageField in a Django abstract model?

I have an abstract Django model and I want to define an ImageField inside of it. The problem is that when I do define it, it raises an error because I don't have an "upload_to" attribute. Is there a way to still define it and have a separate folder for each inheriting class? ...

Django ModelAdmin.save_model() changes not committing to database

I have a many to many relationship between Group and User and also between User and Domain. Also i have one to many relationship between Domain and Group. I have defined the save_model function of UserAdmin as follows: def save_model(self, request, obj, form, change): form.save(False) obj.save() form.save_m2m() ...

Model Method with a Variable

I have the following view that takes the value of "q" from a template: from django.http import HttpResponse from django.shortcuts import render_to_response from GOTHAMWEB.GRID.models import * def search(request): errors = [] if 'q' in request.GET: q = request.GET['q'] if not q: errors.append('Enter a...

Django ManyToManyField Creation Problems

I currently have these models: class Category(models.Model): name = models.CharField(max_length=200) parent = models.ForeignKey('self', blank=True, null=True, related_name='child') description = models.TextField(blank=True,null=True) class Item(models.Model): name = models.CharField(max_length=500) ... tag = mod...

Can you/should you modify objects in the database in the model of other classes

I want to perform some delete() and some save() methods on some objects that are not an instance of the current class I'm in. I'm trying to do this in an overloaded save() method of a class. Here is the scenerio: class Item(models.Model): name = models.CharField(max_length=500) category = models.ForeignKey(Category, null=True,...

New field in Django model doesn't show up in admin interface or model forms

I've created a model in one of my apps which works fine. However, I needed to add a new field. I did this, and used manage.py reset <appname> to drop the tables and add them again. This process went fine - the new field appears in the database. However, I can't get the field to show up in the admin interface, nor in the custom model form...

How/When does Django calculate the ForeignKey object

I have a models class class A(models.Model): user = models.ForeignKey(User) Now if I do a a = A.objects.get(pk = something_existing); print a.__dict__, user is not in __dict__. a.user however doesnot give an attribute error. So when is the actual user being calculated? I looked in django.db.models.base.Model and django.db.models.base.M...

Disable HTML escaping in Django's TextField

How can I turn off Django's automatic HTML escaping, when I write into model's TextField? ...

Filtering inherited objects

Hi all, I have a base class A . Two derived classes B , C. Now I have a global class witch contains a many-to-many relation to object A. Class D: aObjects : ManyToMany("A") how could know the real object the filter query return in object D. I mean : d.objects.get(id=5) now d has n objects of class A, but they are a mix of A,B or ...

What method/property in a django field is called from the template?

I want to subclass models.TextField so I can return the text to the template with occurences of \r\n replaced with <br />. I don't want the <br />'s stored in the database, however. What method is called to retrieve the field's data when invoked from the template? I tried this, but it doesn't seem to work: class HTMLTextField(models.Te...

Getting from a model to the model type's verbose name

I've got an instance of a model FooBar - how do I get the naturalised type Foo Bar for display to the user, making sure to use the class's verbose_name if one is set in the Meta options. e.g.: class FooBar(models.Model): title = models.CharField(max_length = 100) class Meta: verbose_name = 'Spaghetti Monster' How do I get 'Sp...

Django - relation with class model instances

Hello I made some model classes with pets types: Parrot, Rabbit, Cat, Dog and i need make model Shop, where one field will be related for few of this models. This field will show what sells in shop. Can i make relation beetween one model object to few model classes? If i can't, how i need change my scheme? ex: 1 Shop1 [Parrot, Rabbit]...

Hidden field in Django Model

A while back I made a Model class. I made several ModelForms for it and it worked beautifully. I recently had to add another optional (blank=True, null=True) field to it so we can store some relationship data between Users. It's essentially a referral system. The problem is adding this new field has meant the referral field shows up wh...

Django: Why is __unicode__ not run during print(some_model_instance)?

This is my class: class Account(models.Model): name = models.CharField(max_length=255) def __unicode__(self): return '{0}'.format(self.name) class Painting(models.Model): account = models.ForeignKey(Account) color = models.CharField(max_length=255) def __unicode__(self): return 'Account: {0} - Co...

Django Generic Relations with Django Admin

heya, I have a Django project, that has a "Address" model. This is used in several places - by a "User Profile" model, by a "Hospital" model, by an "Insitution" model etc. I'm using Django's generic relations to allow each of these objects to create a foreign-key to Address. However, this seems to cause some weirdness in the Django Ad...

Is a reason I can't add a ManyToManyField?

So I'm building a Django application, and these are a few models I have: class MagicType(models.Model): name = models.CharField(max_length=155) parent = models.ForeignKey('self', null=True, blank=True) class Spell(models.Model): name = models.CharField(max_length=250, db_index=True) magic_words = models.CharField(ma...

Django ForeignKey issue

I have two classes class A(models.Model): id=models.IntegerField(primary_key=True) name=models.CharField(max_length=200) store_id=models.IntegerField() type=models.ForeignKey(B) class B(models.Model): id=models.IntegerField(primary_key=True) type=models.CharField(max_length=10) class C(models.Model): id=mo...

Iterating over a large Django queryset while the data is changing elsewhere

Iterating over a queryset, like so: class Book(models.Model): # <snip some other stuff> activity = models.PositiveIntegerField(default=0) views = models.PositiveIntegerField(default=0) def calculate_statistics(): self.activity = book.views * 4 book.save() def cron_job_calculate_all_book_statistics(): ...