Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango model.save() not writing to database
    primarykey
    data
    text
    <p>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.</p> <p>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).</p> <p>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.:</p> <p><strong>addNewEvent()</strong> (in views.py)</p> <pre><code>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() </code></pre> <p><strong>models.py</strong> </p> <pre><code>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' </code></pre> <p><strong>forms.py</strong></p> <p>from django.forms import ModelForm, forms from models import Event, Location, EventSchedule</p> <pre><code>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') </code></pre> <p>Any help is much appreciated.</p> <p>Thank you,</p> <p>Peter</p> <p><strong>EDIT:</strong></p> <p>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:</p> <pre><code>new_user_event = UserEvent(user=user, event=new_event, datetime_created=datetime.datetime.now()) new_user_event.save() </code></pre> <p>Any ideas?</p> <p><strong>EDIT 2:</strong></p> <p>Screenshot of new_user_event: <a href="http://imgur.com/JUauG" rel="nofollow">http://imgur.com/JUauG</a></p> <p><strong>EDIT 3:</strong></p> <p>This is how I'm instantiating the ModelForms:</p> <pre><code>event_form = EventForm(request.POST or None) location_form = LocationForm(request.POST or None) event_sched_form = EventScheduleForm(request.POST or None) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload