I’m new to Django and just trying to submit a form using ModelForms to create a new event and save it to the database.
I am trying to insert rows into three tables: Location, Event (which has a location_id FK), and EventSchedule (which as an event_id FK).
Here is the method that saves the forms. When it is called, no errors are returned, yet the new rows are not inserted into the database.:
addNewEvent() (in views.py)
def addNewEvent(self, event_form, location_form, event_sched_form, latitude, longitude):
new_event = event_form.save(commit=False)
new_location = location_form.save(commit=False)
new_event_sched = event_sched_form.save(commit=False)
# set any necessary values and save forms
new_location.latitude = latitude
new_location.longitude = longitude
new_location.save()
new_event.flagged = False
new_event.location = new_location
new_event.save()
new_event_sched.event = new_event
new_event_sched.save()
models.py
class Location(models.Model):
name = models.CharField(max_length=200)
#address = models.CharField(max_length=200)
city = models.CharField(max_length=200)
state_code = models.CharField(max_length=100)
zip = models.CharField(max_length=100, blank=True)
country = models.CharField(max_length=100)
latitude = models.DecimalField(max_digits=9, decimal_places=6)
longitude = models.DecimalField(max_digits=9, decimal_places=6)
class Meta:
db_table = 'location'
class Event(models.Model):
CATEGORIES = (
('KID', 'Kids'),
('FAM', 'Family'),
('NIT', 'Nightlife'),
('OUT', 'Outdoors'),
('ART', 'Arts'),
('MSC', 'Music'),
('SPT', 'Sports'),
('SAL', 'Sale'),
('EDU', 'Educational'),
('POL', 'Political'),
('FOD', 'Food'),
('OTH', 'Other')
)
location = models.ForeignKey(Location)
name = models.CharField(max_length=200)
description = models.CharField(max_length=200)
category = models.CharField(max_length=20, choices=CATEGORIES)
age_req = models.IntegerField(null=True)
cost_per_person = models.DecimalField(max_digits=5, decimal_places=2, null=True)
flagged = models.BooleanField()
users = models.ManyToManyField(User, through='UserEvent')
class Meta:
db_table = 'event'
class EventSchedule(models.Model):
event = models.ForeignKey(Event)
start_date_time = models.DateTimeField()
end_date_time = models.DateTimeField()
class Meta:
db_table = 'event_schedule'
forms.py
from django.forms import ModelForm, forms
from models import Event, Location, EventSchedule
class EventForm(ModelForm):
class Meta:
model = Event
fields = ('name','description','category','age_req','cost_per_person')
class LocationForm(ModelForm):
class Meta:
model = Location
fields = ('name','city','state_code','zip','country')
class EventScheduleForm(ModelForm):
class Meta:
model = EventSchedule
fields = ('start_date_time', 'end_date_time')
Any help is much appreciated.
Thank you,
Peter
EDIT:
I can’t even save a Model, let alone a ModelForm (even though I am doing it successfully in other places). I added the following code to associate a user with an event. No errors, yet it doesn’t add a row to the database:
new_user_event = UserEvent(user=user,
event=new_event,
datetime_created=datetime.datetime.now())
new_user_event.save()
Any ideas?
EDIT 2:
Screenshot of new_user_event: https://i.stack.imgur.com/mHnmR.jpg
EDIT 3:
This is how I’m instantiating the ModelForms:
event_form = EventForm(request.POST or None)
location_form = LocationForm(request.POST or None)
event_sched_form = EventScheduleForm(request.POST or None)
Wow, I am such an idiot. I was deleting the rows in the tables before I could actually display the events due to some code that populated the database with dummy data.
Can I delete this question or should it live on as a testament to my stupidity?