Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango- populating an "update/edit" form with an autofield and foreignkey
    primarykey
    data
    text
    <p>I'm new to Django and I'm creating an app to create and display employee data for my company. Currently the model, new employee form, employee table display, login/logout, all works. I am working on editing the current listings. I have hover on row links to pass the pk (employeeid) over the url and the form is populating correctly- except the manytomanyfields are not populating, and the pk is incrementing, resulting in a duplicate entry (other than any data changes made).</p> <p>I will only put in sample of the code because the model/form has 35 total fields which makes for very long code the way i did the form fields manually (to achieve a prettier format).</p> <pre><code>#view.py #SEE EDIT BELOW FOR CORRECT METHOD @login_required def employee_details(request, empid): #empid passed through URL/link obj_list = Employee.objects.all() e = Employee.objects.filter(pk=int(empid)).values()[0] form = EmployeeForm(e) context_instance=RequestContext(request) #I seem to always need this for {%extend "base.html" %} to work correctly return render_to_response('employee_create.html', locals(), context_instance,) #URLconf (r'^employee/(?P&lt;empid&gt;\d+)/$', employee_details), # snippets of employee_create.html. The same template used for create and update/edit, may be a source of problems, they do have different views- just render to same template to stay DRY, but could add an additional layer of extend for differences needed between the new and edit requests EDIT: added a 3rd layer of templates to solve this "problem". not shown in code here- easy enough to add another child template {% extends "base.html" %} {% block title %}New Entry{% endblock %} {% block content %} &lt;div id="employeeform"&gt; {% if form.errors %} &lt;p style="color: red;"&gt; Please correct the error{{ form.errors|pluralize }} below. &lt;/p&gt; {% endif %} &lt;form action="/newemp/" method="post" class="employeeform"&gt;{% csrf_token %} #SEE EDIT &lt;div class="left_field"&gt; {{ form.employeeid.value }} {{ form.currentemployee.errors }} &lt;label for="currentemployee" &gt;Current Employee?&lt;/label&gt; {{ form.currentemployee }}&lt;br/&gt;&lt;br/&gt; {{ form.employer.errors }} &lt;label for="employer" class="fixedwidth"&gt;Employer:&lt;/label&gt; {{ form.employer }}&lt;br/&gt; {{ form.last_name.errors }} &lt;label for="last_name" class="fixedwidth"&gt;Last Name:&lt;/label&gt; {{ form.last_name }}&lt;br/&gt; {{ form.facility.errors }} #ManyToMany &lt;label for="facility" class="fixedwidth"&gt;Facility:&lt;/label&gt; {{ form.facility }}&lt;br/&gt;&lt;br/&gt; &lt;/div&gt; &lt;div id="submit"&gt;&lt;br/&gt; &lt;input type="submit" value="Submit"&gt; &lt;/div&gt; &lt;/form&gt; #models.py class Employee(models.Model): employeeid = models.AutoField(primary_key=True, verbose_name='Employee ID #') currentemployee = models.BooleanField(null=False, blank=True, verbose_name='Current Employee?') employer = models.CharField(max_length=30) last_name = models.CharField(max_length=30) facility = models.ForeignKey(Facility, null=True, blank=True) </code></pre> <p>base.html just has a header on top, a menu on the left and a big empty div where the forms, employee tables, etc all extend into.</p> <p><a href="http://tinypic.com/r/2em0ql3/7" rel="nofollow" title="screenshot2">screenshot2</a></p> <p>So, how do I need to change my view and/or the in the template to update an entry, rather than creating a new one? ( And how do I populate the correct foriegnkeys? (the drop down boxes have the right options available, but the "-----" is selected even though the original database entry contains the right information.</p> <p>Let me know if i need to include some more files/code I have more pics too but i cant link more or insert them as a new user :&lt; I'll just have to contribute and help out other people! :D</p> <p>EDIT: I've been working on this more and haven't gotten too far. I still can't get the drop-down fields to select the values saved in the database (SQLite3). But the main issue I'm trying to figure out is how to save as an update, rather than a new entry. save(force_update=True) is not working with the default ModelForm save parameters.</p>
    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.
 

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