django-models

how to set the default value for grouped choices in django

If I have some grouped choices for a models.IntegerField, how can I set the default value to a combination of those choices ex: class ForumThread(): STATE_CHOICES = ( ('Sticky', ( (True, 'True'), (False, 'False') ) ), ('Blocked', ( (False, 'False') (True, 'Tru...

How to use Django ORM to get a list by year of all articles with an article count

Hello, I am trying use the django ORM to get a list by year of all my articles with an article count beside it, such as this: 2010 (5 articles) 2009 (4 articles) 2008 (9 articles) I have tried things such as: archive = Articles.objects.dates('created', 'year').annotate(archive_count=Count('created')) or: archive = Articles.objects.v...

Showing the Foreign Key value in Django template

Hello everyone. Here is my issue. I am new to python/django (about 2 months in). I have 2 tables, Project and Status. I have a foreign key pointing from status to project, and I am looking to try to display the value of the foreign key (status) on my project template, instead of the address of the foreign key. Here is my models.py...

How correctly override ModelForm's save() method for a model with tags?

Consider I have defined the following models: class Tag(models.Model): name = models.CharField(max_length=20) class Entry(models.Model): title = models.CharField(max_length=100) date = models.DateField() tags = models.ManyToManyField(Tag) and the following model form: class EntryForm(forms.ModelForm): tags = CharField(max...

FK not populating in django form

Hello everyone. I have a small question to ask the community. I am wondering why my foreign key will not show up when I go to my edit form. When I go to edit the information, all my data is populated except for status (which is a foreignkey that points from the status table to project table), which is left with nothing selected. I ...

Django - Need some guidance on how to implement a ManyToMany database

I want the ability to let users indicate what countries they have visited.. my models.py looks something like this: class User(models.Model): name = models.CharField(max_length=50) countries = models.ManyToManyField(Countries) class Countries(models.Model): #This is where I don't know what to do. #The end goal i...

Subclassing the Django ImageFileField

I'm interested in subclassing django's ImageFileField to allow access to the image IPTC metadata, something like: >>> from myapp.models import SomeModel >>> obj = SomeModel.objects.all()[0] # or what have you >>> obj.image.iptc['keywords'] ('keyword','anotherkeyword','etc') ... the docs say to read over django's internal code, which I...

Diffrent models sharing one ID list

In my application I've added "Facebook Comment Box" on different pages for different objects. Each object has it's own comments list so I need to provide a unique (across the site) xid for every single one of them. So what would be the best approach for achieving this ? Some abstract model that all other models will inherit ? Dummy model...

Import csv data into database in Django Admin

I've tried to import a csv file into a database by tweaking the modelform inside the admin doing this: models.py: class Data(models.Model): place = models.ForeignKey(Places) time = models.DateTimeField() data_1 = models.DecimalField(max_digits=3, decimal_places=1) data_2 = models.DecimalField(max_digits=3, decimal_place...

Django model instance specific code

Hi, in one of my Django models I have to add specific code for each model instance. Now, I wonder what would be a good way to implement this. My current attempt results in a big, hard to read if statement. Consider the following model: class Foo(models.Model): name = models.CharField(max_length=255) def do_instance_specific_s...

timezone Datatype in django

class MyModel(models.Model): timezone = models.CharField() In the above,should timezone be stored in a char field for the below values, from pytz import all_timezones for t in all_timezones: print t Africa/Abidjan Africa/Accra ---- ---- ...

Django form QuerySet for ModelChoiceField

I've got a Form which I'm using the following field in. contact_country = forms.ModelChoiceField(queryset=Country.objects.all()) The Country model looks like this class Country(models.Model): iso = models.CharField(max_length=2) name = models.CharField(max_length=80) printable_name = models.CharField(max_length=80) is...

How do you display image objects in a Django template using a custom template tag? (coding included)

I have the following code that fails to display object images. But displays normal images fine. My Model class News(models.Model): title----------- image = models.ImageField(upload_to='images') body------------ Template tag coding from django import template register = template.Library() from ----.models import --- def funct(num): ...

Django ImageField: files dont get uploaded...

Hello, I implemented some ImageFields in my model and installed PIL (not the cleanest install). Things seem to work as I get an upload button in the admin and when I call the .url property in the view I get the string with the filename + its upload property. The problem is that the file is not there, apparently it doesnt get uploaded onc...

Structuring Django Many-to-Many Relation

In writing an application for my school's yearbook committee, I've hit a bit of a dead end with modeling a specific relation. Currently I have a photo class class Photo(models.Model): photo = models.ImageField(upload_to="user_photos/") name = models.CharField(blank=True, max_length=50) rating = models.IntegerField(default=1000) wi...

Django: Field.to_python not called on foreign key IDs?

I've got a subclass of Field: class MyIDField(Field): ... def to_python(self, value): return to_base_36(value) And I use it as a primary key like this: class Foo(m.Model): id = MyIDField(primary_key=True) class Bar(m.Model): foo = m.ForeignKey(Foo) Then the MyIDField.to_python function isn't called when I a...

django model save error message

hi, i like to output an error message if this date allready exist. has someone an idea? class Bet(models.Model): name = models.CharField(max_length=30, verbose_name='Name') date = models.DateTimeField(verbose_name='Datum') def __unicode__(self): return self.name def save(self): newDate = self.date try: Bet.objec...

Custom field's to_python not working? - Django

Hi folks, I'm trying to implement an encrypted char field. I'm using pydes for encryption This is what I have: from pyDes import triple_des, PAD_PKCS5 from binascii import unhexlify as unhex from binascii import hexlify as dohex class BaseEncryptedField(models.CharField): def __init__(self, *args, **kwargs): self.td =...

Checking for duplicates

Hello everyone, I have a small problem. I am trying to check to see if status's value already exists and make sure I do not create another instance of it, but I am having some trouble. Ex. If the project status was once "Quote" I do not want to be able make the status "Quote" again. Right now, I check to make sure if the user select...

Using send_mail in Django: Cannot display data in invoice from functions

Hello, another Django send_mail question. Seems like I have problems displaying data in an email that separate form from function. Seems like this is a variable problem. Edit: I manage to make the client name show up! Now how can to the same thing with invoice. Say that I wanted to display the date, invoice_no, work_orders & contract_i...