On Tue, 2008-11-25 at 13:39 +0900, Stephen Moore wrote: > Hello, > > I'm trying to create a model here for essentially recording the > positions held by members in the committee of a university club I'm > creating a website for. > > However, I'm new to django and am having some issues. > > So far, I have : > > A Committee model containing a year and a set of members (committee > can change from year to year) > > Each member has a name, what degree they're in, a description of them > and a set of positions they've held. (position in the committee for > each member can change from year to year) > > Each position has the year and type of position > > and the type of position has a title and a description. > > which results in the followiing code in my models.py > > class Committee(models.Model): > year = models.IntegerField() > members = models.ForeignKey('Member') > > class Member(models.Model): > name = models.CharField(max_length=50) > degree = models.CharField(max_length = 50) > desc = models.TextField() > position = models.ForeignKey('Position') > > class Position(models.Model): > year = models.IntegerField() > pos_type = models.OneToOneField('PositionType') > > class PositionType(models.Model): > title = models.CharField(max_length=50) > desc = models.TextField() > > Then in my admin.py, I have the following for the member interface > > class MemberAdmin(admin.ModelAdmin): > fieldsets = [ > (None, {'fields':['name', 'degree', 'desc']}) > ] > > admin.site.register(Member, MemberAdmin) > > Then when I add a member, I get an error page saying > "main_member.position_id may not be NULL" > > as well as this, if I then try and add the ability add positions by doing : > > class positionInline(admin.TabularInline): > model = Position > extra = 3 > > class MemberAdmin(admin.ModelAdmin): > fieldsets = [ > (None, {'fields':['name', 'degree', 'desc']}) > ] > inlines = [positionInline] > > admin.site.register(Member, MemberAdmin) > > then when I try and add a member, I get an error saying > "<class 'cec.main.models.Position'> has no ForeignKey to <class > 'cec.main.models.Member'>"
The error message is telling you exactly what the problem is here. It wants Position to have a ForeignKey to Member. When you read that and look at your models and see that you have Member having a ForeignKey to Position, you have to then think that perhaps that means you've got the inline direction going the wrong way. There's nothing in the documentation to say it's a bidirectional option. ForeignKey is a many-to-one option: many members for one position. Inline editing exists so that you can edit these "many" members inline with the single position that they all relate to. It isn't needed for the other direction. You don't seem to have tried the obvious solution here: include "position" in the list of fields for Member's admin. That will do exactly what you're after. Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---