2👍
✅
You should have a look over the django-docs
You need to create a Calendar
instance first.
Somewhat like this:
title= 'traction'
macategorie= 'Atomisation'
moncalendar = Calendar.objects.create(name="Atomisation", slug="atomisation")
p = Event(title= title, calendar= moncalendar)
EDIT
If you want to make sure, that the Calendar
object is unique by the slug, try:
moncalendar, created = Calendar.objects.get_or_create( slug="atomisation")
moncalendar.name = "Atomisation"
moncalendar.save()
For this to work, you would need to change your model to something like this:
name = models.CharField(_('name'), max_length=50, blank=True, default="")
or
name = models.CharField(_('name'), max_length=50, blank=True, null=True)
or you would put it in an try-except and handle the case, where you try to add a Calendar with a same slug explicitly.
Somehow like this:
try:
moncalendar = Calendar.objects.create(name="Atomisation", slug="atomisation")
except IntegrityError:
moncalendar = Calendar.objects.get(slug="atomisation")
moncalendar.name = "Atomisation" # or handle differently as you like
moncalendar.save()
See here for more information.
Source:stackexchange.com