Tuesday, August 28, 2018

Re: ManyToMany relationship with through_fields and Admin

What changes you did ?

Regards,
Akash Kandpal.

On Wed, Aug 29, 2018, 12:34 AM Vincent <vincent@minon.be> wrote:
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)



Le mardi 28 août 2018 00:44:49 UTC+2, Vincent a écrit :
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

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CAPND-h6%2BfhTXF_vzKLjHofXjg%2BTWrD0NpyefaVKW0Q4q55%3DbPg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment