|
Posted by stank345 |
|
|
I asked this question over at stackoverflow.com (http://stackoverflow.com/questions/11251255/django-model-save-not-writing-to-database), but didn't get a good answer so I thought I'd ask it in a more specific forum.
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 four tables: Location, Event (which has a location_id FK), EventSchedule (which as an event_id FK), and UserEvent (which as an event_id FK as well). 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, user):
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()
new_user_event = UserEvent(user=user,
event=new_event,
datetime_created=datetime.datetime.now())
new_user_event.save()
event_form.save_m2m() # not sure if this line is necessary!
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'
class UserEvent(models.Model):
user = models.ForeignKey(User)
event = models.ForeignKey(Event)
datetime_created = models.DateTimeField()
class Meta:
unique_together=(("user", "event"))
db_table = 'user_event'
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')
Now, 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: Screenshot of new_user_event: http://imgur.com/JUauG Any help is much appreciated. Thank you, Peter |
|
|
Posted by nirupama |
|
|
how to use modelchoicefield for id field which is foreign key for populating combobox
|


