ManyToMany relationship with through_fields and Admin

219 views
Skip to first unread message

Vincen

unread,
Aug 27, 2018, 6:44:49 PM8/27/18
to Django users
Hello,

I'm new to Django and i'm trying to made a simple app in which users can attend to an event. I'm trying to have this manageable through the admin site but i get the following error :
<class 'evenement.admin.AttendInline'>: (admin.E202) fk_name 'attendees' is not a ForeignKey to 'evenement.Events'.

My goal is to have an interface to create the event and add users in the event.

Here are my models :

from django.db import models
from django.contrib.auth.models import User


# Create your models here.
class Events(models.Model):
 
Name = models.CharField(max_length=64)
 
Date = models.DateTimeField()
 
Description = models.CharField(max_length=200)
 
Admin = models.ForeignKey(User, models.SET_NULL, blank=True, null=True, related_name='event_admin')
 
Status = models.PositiveIntegerField(default=0)
 
Attendees = models.ManyToManyField(User, through='Attend',through_fields=('events','attendees'))
 
 
def __str__(self):
 
return self.Name


class Attend(models.Model):
 events
= models.ForeignKey(Events, on_delete=models.CASCADE)
 attendees
= models.ForeignKey(User, on_delete=models.CASCADE)
 
PlusOne = models.ForeignKey(User, on_delete=models.CASCADE, related_name='PlusOne')

And the admin.py

from django.contrib import admin
from .models import Events
from .models import Attend
from django.contrib.auth.models import User




# Register your models here.


class AttendInline(admin.TabularInline):
 model
= Attend
 fk_name
= "attendees"
 extra
= 1


class UserAdmin(admin.ModelAdmin):
 inlines
= [ AttendInline, ]


class EventsAdmin(admin.ModelAdmin):
 inlines
= [ AttendInline, ]




admin
.site.unregister(User)
admin
.site.register(User, UserAdmin)


admin
.site.register(Events, EventsAdmin)


Your help will be much appreciated !

Thank you

Vincent

Vincent

unread,
Aug 28, 2018, 3:04:07 PM8/28/18
to Django users
I figured it out :

from django.db import models
from django.contrib.auth.models import User


# Create your models here.
class Events(models.Model):
 
Name = models.CharField(max_length=64)
 
Date = models.DateTimeField()
 
Description = models.CharField(max_length=200)
 
Admin = models.ForeignKey(User, models.SET_NULL, blank=True, null=True, related_name='event_admin')
 
Status = models.PositiveIntegerField(default=0)
 
Attendees = models.ManyToManyField(User, through='Attend',through_fields=('events','attendees'))
 
 
def __str__(self):
 
return self.Name




class Attend(models.Model):
 events
= models.ForeignKey(Events, on_delete=models.CASCADE)

 attendees
= models.ForeignKey(User, on_delete=models.CASCADE, related_name='attendees')

 
PlusOne = models.ForeignKey(User, on_delete=models.CASCADE, related_name='PlusOne')

 
 
class Meta:
 unique_together
= (('events', 'attendees'),)

from django.contrib import admin
from .models import Events
from .models import Attend
from django.contrib.auth.models import User




# Register your models here.


class AttendInline(admin.TabularInline):
 model
= Attend

 extra
= 1



class EventsAdmin(admin.ModelAdmin):
 inlines
= [ AttendInline, ]





admin
.site.register(Events, EventsAdmin)

akash kandpal

unread,
Aug 28, 2018, 3:54:05 PM8/28/18
to django...@googlegroups.com
What changes you did ?

Regards,
Akash Kandpal.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/690d5956-6348-4ae3-99bf-030e7df91cdb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages