django-models

Django error: 'NoneType' object is not subscriptable

It's taking me way to long to make this simple form. Almost there but when I submit I get the NoneType error views.py: from djangoproject1.authentication import forms from django.contrib.auth.models import User from django.http import HttpResponseRedirect from django.shortcuts import render_to_response def main(request): rf = for...

Django - how to access fields in a customized many-to-many through object in templates

Consider the following models: class Person(models.Model): name = models.CharField(max_length=128) class Group(models.Model): name = models.CharField(max_length=128) members = models.ManyToManyField(Person, through='Membership') class Membership(models.Model): person = models.ForeignKey(Person) group = models.Forei...

Can model properties be displayed in a template

I am looking to display a model property in a template, which uses an inlineformset_factory. Is this even possible? I have not come across any example. I am trying to display 'json_data' in my template class RecipeIngredient(models.Model): recipe = models.ForeignKey(Recipe) ingredient = models.ForeignKey(Ingredient) serving...

Admin site automatically get current user

I am making a blog application and want to automatically add the current user when i submit a new post through the admin site. Is there any way that i could detect the current logged-in user and add it to the post. These are the models: class Post(models.Model): user = models.ForeignKey(User) title = models.CharField('Title', ma...

many-2-many table requires an id field?

It seems to me that the current django release (starting with 1.2) requires a many-2-many join table to have its own id field and that this key is its primary key. Is that true? I have some applications which are using legacy databases in which the many-2-many table have primary keys consisting of both foreign keys. But those applicatio...

How can I sort my Django model using complex data from a second model?

I have 3 django models (simplified for this example): class Fighter (models.Model): name = models.CharField(max_length=100) weight_class = models.ForeignKey(WeightClass, related_name="fighter_weight_class") class Bout (models.Model): fighter_1 = models.ForeignKey(Fighter, related_name="bout_fighter_1") fighter_2 = model...

How can I create sophisticated Django Model Validation for Django Admin?

I have the following model in Django: class Bout (models.Model): fighter_1 = models.ForeignKey(Fighter, related_name="bout_fighter_1") fighter_2 = models.ForeignKey(Fighter, related_name="bout_fighter_2") winner = models.ForeignKey(Fighter, related_name="bout_winner", blank=True, null=True, help_text='Leave blank fo...

How to import datetime module in Django?

I've learned some basic in Django but I stucked in this tutorial: http://docs.djangoproject.com/en/dev/intro/tutorial02/#customize-the-admin-change-list Error found when was_published_today added: global name 'datetime' is not defined Some search results suggest me to import datetime module, but I have no idea how to do it. from d...

Django model iterate fields

Hi all, how can I iterate and retrieve all fields of a django model? I know that foo.item._meta.get_all_field_names() brings me all field names. How can I access those fields (incl. their actual values) on a model instance? (Except the normal notation foo.fieldname). I need this in order to build a custom output for my model including...

Django - Get items in many sets

My models: class ItemSet(models.Model): name = models.CharField(max_length=30) item = models.ManyToManyField(Item) order = models.IntegerField(default=0) class Item(models.Model): name = models.CharField(max_length=30) desc = models.CharField(max_length=100) A set includes many items and a item can be in many sets...

How do I include a Django app in my PYTHONPATH?

I want to be able to import things from applications in my project without referring to my project name. My folder structure is like so; I have a project called djangoproject1, and inside I have a folder called apps, and then I have my individual apps in that folder. djangoproject1, apps, and all my applications have an empty "__init__...

Django Model design

I needed some help in model design. I wanted a model where a user can associate himself with numerous emails by submitting them from a form. And when the user wants to use the websites contact form, he can choose the email he wants a reply on. Will it be something like this : class Email(models.Model): author = models.ForeignKey(Us...

Getting related

Hi guys, i having problem with this: model.py (1) class Profession(models.Model): user= models.ForeignKey(User,unique=True) principal_area = models.ForeignKey(Area,verbose_name='Area principal',related_name='area_principal') others_areas = models.ManyToManyField(Area) model.py (2) class Area(models.Model): area = mod...

Django - Compare Model Code to Database

I maintain a Django project with a database that has several model constraints that have fallen out of sync with the actual database. So, for example, some model fields have null=False set, but the database permits NULLs for the corresponding database column. I'm curious if there is a utility, either in Django or a third-party Python sc...

How to check value transition in Django (django-admin)? (Revisited)

I was wondering about how to control transitions in model data. I found the solution at http://stackoverflow.com/questions/867120/how-to-check-value-transition-in-django-django-admin but when I tried to implement it within my code, something went wrong (I am able to change the status with impunity): Here is the relevant portion of my ...

Using list_filter with an InLine model in Django

Hello, We are having problems getting the filtering in the admin interface working the way we would like it to. We have the following: models.py: class Person(models.Model): first_name = models.CharField(blank=True) last_name = models.CharField(blank=True) class Affiliation(models.Model): person = models.ForeignKey(Person) a...

Is having everything in Django models sensible?

I've just inherited a Django project for maintenance and continuous development. While I'm a fairly proficient programmer (also Python) I have next to no experience with Django, therefore I need a bit of saneness-checking for my ideas ;) The current problem is this: the project contains a custom install.sh file, which does three things:...

django model Charfield

If I want a CharField with only one digit is this the way to write the model variable = model.CharField(max_length=1) Or if you only have one Char it is a differnet field? Thanks! ...

Django chat App..

How to integrate http://ajaxim.com/ to a django module Small piece of code would be helpful ...

Django Model ForeignKey

If I want to add a Foreignkey to a model from another class in the same model e.g. class1 (models.Model): variable1 = models.IntegerField() class2 (models.Model): variable2 = models.CharField() foreignkey = models.Foreignkey(class1.variable1) Is that possible? Does that make sense as a programming move? This ForeignKey w...