Hi,

I've just started using both python and django, so bear with me if I'm
making a newbie mistake. I've done a small example model and want to
be able to administer it through the built-in admin interface in
django.

First I've expanded the user model in django, as described in [1] to
add some extra data fields (e.g. city, zipcode, etc.). And then I've
added a Newsletter class and a relation between newsletter and a user
to be able to control which users should receive which newsletters.

In the admin interface I want to be able to edit the newsletter
receivers inline when I edit/create a newsletter, I've added code in
admin.py, according to [2], which should accomplish this. But when i
login to the admin interface and add a newsletter, all I see are the
fields to add a title and content to the newsletter. Does anyone have
an idea of what I'm missing or doing wrong, in order to accomplish
this? The code in admin.py and models.py can be seen below

[1] = http://www.djangobook.com/en/1.0/chapter12/ (at the bottom)
[2] = 
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#working-with-many-to-many-intermediary-models

<quote=models.py>
from django.db import models
from django.contrib.auth.models import User
import datetime

class ShopProfile(models.Model):
  #One-to-one mapping to an existing user
  user = models.ForeignKey(User, unique=True)

  #Additional data we need on a user
  street = models.CharField(max_length=100)
  city = models.CharField(max_length=100)
  zipcode = models.CharField(max_length=10)
  home_phone = models.CharField(max_length=25)
  mobile_phone = models.CharField(max_length=25)
  subscribed_to_newsletter = models.BooleanField(default=False)
  def __unicode__(self):
    return self.user.first_name + " " + self.user.last_name

class Newsletter(models.Model):
  title = models.CharField(max_length=200)
  content = models.TextField(blank=True)
  date_sent = models.DateTimeField(blank=True, null=True)
  sent = models.BooleanField(default=False)
  receivers = models.ManyToManyField(User,
through='NewsletterReceiver')
  def __unicode__(self):
    return self.title

class NewsletterReceiver(models.Model):
  user = models.ForeignKey(User)
  newsletter = models.ForeignKey(Newsletter)
  sent_to_user = models.BooleanField(default=False)
</quote>

<quote=admin.py>
from dshop.newsletter.models import Newsletter, NewsletterReceiver,
ShopProfile
from django.contrib import admin

class NewsletterReceiverInline(admin.TabularInline):
  model = NewsletterReceiver
  extra = 1

class NewsletterAdmin(admin.ModelAdmin):
  list_display = ('title', 'sent', 'date_sent')
  list_filter = ['sent']

  fieldsets = [
        ('Title',               {'fields': ['title']}),
        ('Content',               {'fields': ['content']}),
    ]
  inline = [NewsletterReceiverInline]

admin.site.register(ShopProfile)
admin.site.register(Newsletter, NewsletterAdmin)
</quote>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to