python - Django creating one-to-one records matching user account -


i creating django application needs one-to-one relationship between user , informational record.

the application meant allow users maintain single record organization.

i've been struggling understanding how enter data through form. i've tried many different approaches, here recent attempt.

the workflow user is:

  1. login , redirected member view.
  2. if organization profile exists, display it. if organization profile not exist, redirect edit view.
  3. in edit view, if request contains post data, add organization profile , save it. if not contain post data, create instance allow user enter data.

a blank record created in database, , form generates correctly, results of form not being saved database.

i think what's tripping me correctly creating record , storing results through foreign key relationship, i've tried can think of.

all appreciated.

models.py

from django.contrib.auth.models import user  class program(models.model):     user = models.onetoonefield(user)     unit_name = models.charfield(max_length=50, verbose_name='name')     ... 

views.py:

def member(request):     if request.user.is_authenticated():         current_user = request.user         record_data, record_exists = program.objects.get_or_create(user=current_user)          if record_exists:             return render(request, 'programs_dbase/member.html', {'record_data': record_data})         else:             return render(request, 'programs_dbase/member.html', {'record_exists': record_exists})      elif request.user.is_authenticated() false:         return httpresponseredirect('/login/')   def edit(request):     if request.user.is_authenticated() , request.method == 'post':         current_user = request.user         record_data = program.objects.get(user=current_user)         form = programform(request.post, instance=record_data)          if form.is_valid():             form.save()          return httpresponseredirect('/member/')      elif request.user.is_authenticated() , request.method != 'post':         current_user = request.user         record_data = program.objects.get(user=current_user)         program_info = programform(request.post, instance=record_data)          return render(request, 'programs_dbase/edit.html', {'program_info': program_info})      else:         return redirect('login') 

update 1: adding html form , forms.py

html:

<div class="form-group">     <form action="/edit/" method="post">         {% csrf_token %}         <h4>name</h4>         <label for="unit_name">name</label> {{ program_info.unit_name }}<br />         <!-- snip -->         <button type="submit" class="btn btn-success">submit</button>     </form> </div> 

forms.py

from django import forms models import program   class programform(forms.modelform):      class meta:         model = program         fields = '__all__' 

update 2: adding print command in views.py evaluate form results

added "print form" in views.py before "if form.is_valid():" , can see result in terminal:

<tr>   <th>     <label for="id_user">user:</label>   </th>   <td>     <ul class="errorlist">       <li>this field required.</li>     </ul>     <select id="id_user" name="user">       <option value="" selected="selected">---------</option>       <option value="1">twitch</option>     </select>   </td> </tr> 

get after adding "print form.errors":

<ul class="errorlist"><li>user<ul class="errorlist"><li>this field required.</li></ul></li></ul> 

update 3: specifying user initial form:

tried changing this:

program_info = programform(request.post, instance=record_data, initial={'user': current_user}) 

but 'user' value field not being selected when form generated. wtf; strange.

you're not posting whole form, going off see, user doesn't explicitly select username on form. therefore, need make sure column manually filled view before saving.

def edit(request):     if request.user.is_authenticated() , request.method == 'post':         current_user = request.user         record_data = program.objects.get(user=current_user)         form = programform(request.post, instance=record_data)          if form.is_valid():             instance = form.save(commit=false)             instance.user = current_user             instance.save()          return httpresponseredirect('/member/')      elif request.user.is_authenticated() , request.method != 'post':         current_user = request.user         record_data = program.objects.get(user=current_user)         program_info = programform(request.post, instance=record_data)          return render(request, 'programs_dbase/edit.html', {'program_info': program_info})      else:         return redirect('login') 

of course, thing models.py, set field's editable property false. that's user onetoones, since expectation i'll never need change user field after creation.


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -