Reputation: 137
I need a model with some FloatField feilds, each records of this table should be used with another one (first row indicates minimum and the second one determines maximum). I like the values of these two rows be added or edited simultaneously by user. I'm eager to know how to use django CreateView and UpdateView generic classes or any other tools for this purpose. Please let me know your helpful comments.
Update: this is a part of my codes:
#models.py
class DefinedInfo(models.Model):
user = models.CharField(max_length=30)
bound = models.CharField(max_length=3) #takes 'min' or 'max'
density = models.FloatField(default=0)
weight =models.FloatField(default=0)
....
#views.py
class InfoCreateView(LoginRequiredMixin, CreateView):
model = DefinedInfo
template_name = 'item_new.html'
fields = ['density', 'weight' ...]
def form_valid(self, form):
form.instance.user = self.request.user
form.instance.bound = 'min'
return super().form_valid(form)
class InfoUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = DefinedInfo
fields = ['density', 'weight' ...]
template_name = 'item_edit.html'
login_url = 'login'
def test_func(self):
obj = self.get_object()
return obj.user == self.request.user
#Html template
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<div>
<h5>Define new item</h5>
<div>
<form action="" method="post">{% csrf_token %}
{{ form|crispy }}
<button" type="submit">Save </button>
<button type="reset" onclick="location.href={% url 'profile' %}" >Cancel</button>
</form>
</div>
</div>
{% endblock %}
There is no problem to add or edit individual records, but I prefer to handle "min" and "max" related records at the same time.
Upvotes: 1
Views: 924
Reputation: 103
Generic Class based Views are made to update/create/delet one single object of a Model at a time.
If you want to make some customizations, it might be clearer and easier to write your own views.
Making a UpdateModel(UpdateView) is confusing if it does modify two rows in the DB.
Upvotes: 2