django-models

i18n Django Internationalization and database objects

I'm working in a bilingual project (es/en); for this project I've chosen to use django's i18n internationalization system (and I'm starting to regret it...) Today's problem is the following: for some models, my database stores information like description and es_description, or english_common_name and spanish_common_name (these are att...

Django model form and objects database id

Hi I have a complex django object, which has properties of other class types. This gets like this: class Order: contractor - type Person some other fields.... In my form I'd like to be able to either choose existing Person object from the dropdown or add a new one with a form. I've managed to create forms and appropriate workfl...

django save_m2m() not working.

Utterly stuck trying to update a ManyToManyField field on POST and save. Models.py class Location(models.Model): place = models.CharField(max_length=100) inuse = models.BooleanField() class Booking(models.Model): name = models.CharField(max_length=100, verbose_name="Your name") place = models.ManyToManyField(Location, ...

speeding up mysql queries / mysql views in django

I use the following code to select popular news entries (by date) from the database: popular = Entry.objects.filter(type='A', is_public=True).extra(select = {'dpub': 'date(dt_published)'}).order_by('-dpub', '-views', '-dt_written', 'headline')[0:5] To compare the execution speeds of a normal query and this one I ran the following mysq...

Django model per table vs model per select

Hey, I am working with Django for a while and now that my "tree" and whole DB is filled with data (note: existing database), I was wondering if the "one model per table" is really better at this point than "one model per select". I have got one table - objtree. This is the place where I have all nodes (brands, categories, tags, etc.) ...

In the Django admin is it possible to separate models into sub-models based on groups?

I think this is easiest to understand as an example: I have models Image and ImageType, where and image has exactly one type. The parameters in ImageType would regulate image size, thumbnail size, etc., as photo gallery images might display differently from, say, profile pictures. I want profile images and gallery images to appear as s...

Manually adding data causes IntegrityError

Hello, I have two Django sites: one for development and one for production. Every once in a while, the data from the development database needs to be transferred to the production database or the other way around. I use postgresql. This works fine: I empty the tables from the database I want to copy to, I generate sql from the applicab...

Adding custom error messages to a field

Is there a way to add a custom error message to a model field without declaring it in the form as a form field? Is this possible? I don't want to declare the field again, for example class MyModel(models.Model): test = models.URLField(max_length = 200) class MyForm(forms.ModelForm): test = forms.URLField(max_length = 200, err...

Representing a multi-select field for weekdays in a Django model

I've been searching for an elegant way to represent a multi-select weekday field (Mon, Tues, Wed...) in a Django model. I was initially thinking of going integer field using bitwise math but I am not sure if this would be the way to go. This would be a mostly-read field. I would want the Queryset method to be something like Entry.object...

Django, What's the best ,fastest way to get only first and last element from something, Customer.objects.xxxx

Django, What's the best ,fastest way to get only first and last element from something, Customer.objects.xxxx such filter, value_list or ... ...

Django, how to group by day

Hello Guys, I've tried the following with no success: Match.objects.filter(sendDate__gte=dateToStats).values("sendDate__day").annotate(perDay=Count("id")).order_by() Fails with: Cannot resolve keyword 'sendDate__day' into field. Where sendDate is a DateTime field, and dateToStats is just a certain date I'm filtering. I'm intereste...

model save method. Resize image before save

Hi. I am trying to resize an image before it gets saved. I am using a custom save method on the model, but I've hit a snag. This is the code I have now: class UserImages(models.Model): height = models.CharField(blank=True, max_length=100) width = models.CharField(blank=True, max_length=100) image = models.ImageField(upload_...

Django model filter, depending on exsisteing relation.

How can I ONLY the categories where there is at least one "Post" related??, hope it makes sense!? **models.py** class Category(models.Model): name = models.CharField(max_length=50) def __unicode__(self): return self.name class Post(models.Model): name = models.CharField(max_length=50) categories = models.ManyToManyFie...

How make QuerySet of items which contain other model.

So i have one model with list of all item i have. And other model which has Foreign-Key to to my main model... How to Make query set object. Of all items from first model which contain my second model? class Music(models.Model): name=models.CharField() duration=models.IntegerField() class Playlist(models.Model): misic=mode...

Getting last created user to link user profile to in django

Hi, I've hit a bit of a wall when trying to add data to a UserProfile model created to hold user info beyond what is catered for in django's built in Auth component. My question is how do I get an instance of the user just registered in order to create the the UserProfile? I thought it would be something like below: # Registration for...

'datetime.date' object has no attribute 'date'

This code: import datetime d_tomorrow = datetime.date.today() + datetime.timedelta(days=1) class Model(models.Model): ... timeout = models.DateTimeField(null=True, blank=True, default=d_tomorrow) ... resuls in this error: 'datetime.date' object has no attribute 'date' What am I doing wrong? ...

Nested inlines in the Django admin?

Alright, I have a fairly simple design. class Update(models.Model): pub_date = models.DateField() title = models.CharField(max_length=512) class Post(models.Model): update = models.ForeignKey(Update) body = models.TextField() order = models.PositiveIntegerField(blank=True) class Media(models.Model): post = mode...

Django, how to update fields in a model without creating a new record?

I have a model in django that I want to update only, that is, when I call it and set the data, it will not create a new record, only update the existing one. How can I do this? Here is what I have: class TemperatureData(models.Model): date = models.DateTimeField() value = models.PositiveIntegerField() alert = models.Boole...

Deletion of a particular record in django

If a model exist like class Empprofile(models.Model): name= models.CharField(max_length=255) group = models.CharField(max_length=255) description = models.CharField(max_length=1024) class Details(Empprofile): address1=models.CharField(max_length=255) address2=models.CharField(max_lengt...

Django - Multiple columns primary key

Hello, I would like to implement multicolumns primary keys in django. I've tried to implement an AutoSlugField() which concatenate my columns values(foreignkey/dates) ... models.py : class ProductProduction(models.Model): enterprise = models.ForeignKey('Enterprise') product = models.ForeignKey('Product') date = models.Dat...