Django form saving all values to 0 when I change just one -
here form question. creating grid of forms populated model data. generated based on code looks "allocation" record user id contains date inside of date range. if no "allocation" record exists field generated 0. upon saving fields have allocations records should updated if changed, , fields don't have allocation records should create allocation record if changed.
the problem: when change number , press save, existing allocation records updated 0.
what doing wrong?
models.py
class resource(models.model): resource_name = models.charfield(max_length=150) weekly_capacity = models.integerfield() class project(models.model): project_name = models.charfield(max_length=150) start_date = models.datefield() end_date = models.datefield() class allocation(models.model): user_id = models.foreignkey(resource) project_id = models.foreignkey(project) week = models.datefield(default=timezone.now) allocated_hours = models.integerfield(default=0) actual_hours = models.integerfield(default=0)
project_profile.py
def resource_allocation_by_week(start,end): users_on_project = allocation.objects.filter(project_id=offset) users_in_date_range = users_on_project.filter(week__range=[start, end]) dict_of_user_hours = {} in users_on_project: total_weeks_allo = 0 in allocation.objects.filter(week__range=[start, end]).filter(user_id=i.user_id): total_weeks_allo += a.allocated_hours allocated_total = i.user_id.weekly_capacity - total_weeks_allo allocated_by_date = users_in_date_range.filter(user_id=i.user_id).values_list('allocated_hours', flat=true) if users_in_date_range.filter(user_id=i.user_id).exists(): allocation_pk = users_in_date_range.filter(user_id=i.user_id).values_list('id', flat=true) f_inst = allocation.objects.get(pk=allocation_pk[0]) allocation_form = edit_resource_allocation(instance = f_inst) dict_of_user_hours[i.user_id] = (allocation_form.as_p, allocated_by_date[0], allocated_total) if request.method == 'post': allocation_form = edit_resource_allocation(request.post, instance = f_inst) if allocation_form.is_valid(): if allocation_form.has_changed(): allocation_form.save() else: non_displayed_form_values = allocation( user_id = i.user_id, project_id = i.project_id, week = start, actual_hours = 0, ) allocation_form_new = edit_resource_allocation(instance=non_displayed_form_values, initial={'allocated_hours' : 0}) dict_of_user_hours[i.user_id] = (allocation_form_new.as_p, 0, allocated_total) if request.method == 'post': allocation_form_new = edit_resource_allocation(request.post, instance=non_displayed_form_values, initial={'allocated_hours' : 0}) dict_of_user_hours[i.user_id] = (allocation_form_new.as_p, 0, allocated_total) if allocation_form_new.is_valid(): if allocation_form_new.has_changed(): allocation_form_new.save()
forms.py
class edit_resource_allocation(modelform): allocated_hours = forms.integerfield(label='', required=false,) class meta: model = allocation exclude = ['user_id', 'project_id', 'week', 'actual_hours']
project_profile.html
<table> <form method="post"> {% csrf_token %} {% obj in resource_allocation_grid %} <tr> <td> {{ obj.0|date:"d m/d/y" }} - {{ obj.1|date:"d m/d/y" }} </td> {% i, v in obj.2.items %} <td> {{v.0}} </td> <td> {{v.2}} </td> {% endfor %} </tr> {% endfor %} <input type="submit" value="save" /> </table>
here end result looks like. (looks good, doesn't work)
needed work prefix
inside <form>
tag. able creating unique string each record regardless of if existed or not.
unique_id = "%s%s" % (start, i.user_id)
thanks @alasdair
note: tried use uuid.uuid4()
generate unique value, discovered each time variable called generate new id. needed use unique id in several places did not solve issue. better have prefix tied data think.
Comments
Post a Comment