django-models

Best way to add convenience methods to a Django Auth User model?

I want to add a convenience/model method to the django.contrib.auth.models.User model. What is the best practice for doing this since, last time I checked, extending the User model was considered bad practice. I have a separate custom UserProfile model. Should I be using that for all User-related convenience methods? ...

Set and use flags against a users profile model in Django.

I have a simple webapp in Django for an iPhone app. I want to prompt the user to review our product, but just once. I then don't want to show that prompt again. So would the best practise way of implementing this to be to add a new entry to the user profile model with a bolean field: "reviewed" - and then set that flag when the user com...

Inheriting specific parent model attributes in foreignkey->'self' relationship in Django

I have a Django model: class Foo(models.Model): name = models.CharField(unique=True) attribute1 = models.FloatField(null=True, blank=True) attribute2 = models.FloatField(null=True, blank=True) attribute3 = models.BooleanField(null=True, blank=True) attribute4 = models.CharField(null=True, blank=True) inherit = mo...

The best way to join two dissimilar mySQL tables -- planning for django from python.

Hi all, table a (t_a): id name last first email state country 0 sklass klass steve [email protected] in uk 1 jabid abid john [email protected] ny us 2 jcolle colle john [email protected] wi us table b (t_b): id sn given nick email l c 0 steven klass ...

Best Practice to get related values in django without DoesNotExist error

If I have two models in Django: class Blog(models.Model): author = models.CharField() class Post(models.Model): blog = models.ForeignKey(Blog) And I want to get all posts for a given blog: Blog.objects.get(author='John').post_set If there is a Blog with author='John' but not posts, a DoesNotExist exception is raised. What...

In python how can I check to see if an object has a value?

Base Account class BaseAccount(models.Model): user = models.ForeignKey(User, unique=True) def __unicode__(self): """ Return the unicode representation of this customer, which is the user's full name, if set, otherwise, the user's username """ fn = self.user.get_full_name() if fn: return fn return sel...

Update model object with new dynamicaly created field?

I have Queryset: queryset = Status.objects.all()[:10] Model Status hasn't got field commentAmount so I would add it to every object in Queryset: for s in queryset: s.commentAmount = s.getCommentAmount() All is fine, print s.commentAmount shows good results, but after: response = HttpResponse() response['Content-Type'] = "text/j...

How can I programmatically obtain the max_length of a Django model field?

Say I have a Django class something like this: class Person(models.Model): name = models.CharField(max_length=50) # ... How can I programatically obtain the max_length value for the name field? ...

Review my Django Model - Need lots of suggestions.

Hi all, I am pulling a variety of information sources to build up a profile of a person. Once I do this I want to the flexibility to look at a person in a different ways. I don't have a lot of expierience in django so I would like a critique (be gentle) of my model. Admittedly even as I coded this I'm thinking redundancy (against DRY...

Django: ManyToMany Inline Admin view error

Here are the model definitions: class ItemBrand(models.Model): name = models.CharField(max_length = 30, unique = True) def __unicode__(self): return self.name class WantedItem(models.Model): name = models.CharField(max_length = 120) description = models.TextField() created = models.DateTimeField(auto_now = False, auto_now_add = Tr...

Is there any way to make Django's USStateField() to not have a pre-selected value?

I use USStateField() from Django's localflavor in one of my models: class MyClass(models.Model): state = USStateField(blank=True) Then I made a form from that class: class MyClassForm(forms.ModelForm): class Meta: model = MyClass When I display the form, the field "State" is a drop-down box with "Alabama" pre-sel...

Django admin: Format fields in list, but keep sortable?

Hi, I keep numeric fields like "size", "width", "height" in my database. Now I would attach units like "KiB" or "pixels" to them when showing them in the change list. This could easily be achieved by adding callables such as "size_formatted" etc to list_display. However, these are no longer sortable. Is there a way around this limitati...

How to filter/exclude inactive comments from my annotated Django query?

I'm using the object_list generic view to quickly list a set of Articles. Each Article has comments attached to it. The query uses an annotation to Count() the number of comments and then order_by() that annotated number. 'queryset': Article.objects.annotate(comment_count=Count('comments')).order_by('-comment_count'), The comments a...

Django name field: is it better to have the whole name in one field?

Hello, Is it better to have 3 fields in django model, "first", "initial" and "last" or is it better to have "name" and put first name, last name and initial in the "name" field? If I put all three in one "name" field can I still search for last names? I am asking because it will be easier to extract the "first, initial, last" from the ...

Django annotate query set with a count on subquery

This doesn't seem to work in django 1.1 (I believe this will require a subquery, therefore comes the title) qs.annotate(interest_level= \ Count(Q(tags__favoritedtag_set__user=request.user)) ) There are items in my query set which are tagged and tags can be favorited by users, I would like to calculate how many ...

How to put an InlineFormSet into a ModelFormSet in Django?

Hi, I'd like to display a number of forms via a ModelFormSet where each one of the forms displays in turn InlineFormSets for all objects connected to the object. Now I'm not really sure how to provide the instances for each ModelFormSet. I thought about subclassing BaseModelFormSet but I have no clue on where to start and would like t...

How do I migrate data from one model to another using South in Django?

I created a Django app that had its own internal voting system and a model called Vote to track it. I want to refactor the voting system into its own app so I can reuse it. However, the original app is in production and I need to create a data migration that will take all the Votes and transplant them into the separate app. How can I ...

Unique model field in Django and case sensitivity (postgres)

Consider the following situation: - Suppose my app allows users to create the states / provinces in their country. Just for clarity, we are considering only ASCII characters here. In the US, a user could create the state called "Texas". If this app is being used internally, let's say the user doesn't care if it is spelled "texas" or "T...

Django: How to initiate/gather field data from two models in one form definition

Hi all, I'm using a Modelform for User to edit first_name and last_name, but I want an extra field from UserProfile (which has a 1-to-1 relation with User) added to this. class UserProfileInfoForm(forms.ModelForm): """ Profile Model field specifications for edit. """ class Meta: model = User fields = ('firs...

Django: Filtering or displaying a model method in Django Admin

I have a model with an expiration DateField. I want to set up an Admin filter that will allow the user to toggle between "Not Expired" and "Any". The model method is quite a simple Date comparison, no problem. However assigning this as a field or filter parameter in the AdminForm is not automatic. Is such a thing possible, and if not...