Answer the question
In order to leave comments, you need to log in
How to change the values of the ForeignKey and ManyToManyField fields on the page?
Hello, I have a stockroom model:
class Stockroom(models.Model):
head = models.ForeignKey('Person')
worker = models.ManyToManyField('Person', through='WorkerType')
goods = models.ManyToManyField('Goods', through='GoodsType')
class WorkerType(models.Model):
person = models.ForeignKey('Person')
stockroom = models.ForeignKey('Stockroom')
type = models.CharField(max_length=150, choices=WORKER_CHOICES, default='NoType')
class GoodsType(models.Model):
goods = models.ForeignKey('Goods')
stockroom = models.ForeignKey('Stockroom')
type = models.CharField(max_length=150, choices=GOODS_CHOICES, default='NoType')
quantity = models.IntegerField(default=0)
Answer the question
In order to leave comments, you need to log in
1. Why do I need to go to another page to select a boss, is it convenient? To get rid of the problems of a drop-down list with a large number of records in the Person table , you can use django-select2 - it will allow you to search by elements or, by clicking on the "delete" button directly in the control, indicate that there is no boss. Demo .
2 and 3. With workers and goods, you should probably use FormSets . Here's an example on how to cross them with class-based views: effectivedjango.com/tutorial/related.html - seems to fit your situation.
PSIf your whole task is to manage tables, have you thought about using the native tool for this - django-admin? There, all this is already there - both the choice and the formsets. Batteries like the Django Suit can spruce up the look. Well, where you need to - customize and add your views, say, to the main page of the admin panel.
Probably already a dead question, but nevertheless, maybe something else will be advised. Or I myself will need it sometime, I will know where to look.
I got it like this:
# forms.py
StockroomWorkerFormSet = inlineformset_factory(Stockroom, WorkerType, extra=0, can_delete=True)
StockroomGoodsFormSet = inlineformset_factory(Stockroom, GoodsType, extra=0, can_delete=True)
# views.py
class StockroomEditView(UpdateView):
model = Stockroom
temlate_name = 'stock/edit.html'
def get_success_url(self):
return self.get_object().get_absolute_url()
def get(self, request, *args, **kwargs):
self.object = self.get_object()
worker_formset = self.get_form(StockroomWorkerFormSet)
goods_formset = self.get_form(StockroomGoodsFormSet)
return self.render_to_response(self.get_context_data(
worker_formset=worker_formset, goods_formset=goods_formset))
def get(self, request, *args, **kwargs):
self.object = self.get_object()
worker_formset = self.get_form(StockroomWorkerFormSet)
goods_formset = self.get_form(StockroomGoodsFormSet)
if worker_formset.is_valid() and goods_formset.is_valid():
return self._form_valid(worker_formset, goods_formset)
else:
return self._form_invalid(worker_formset, goods_formset)
def _form_valid(self, worker, goods):
worker.save()
goods.save()
return HttpResponseRedirect(self.get_success_url())
edit.html
<form action="" method="post">
{% csrf_token %}
{{ worker_formset.management_form }}
{{ goods_formset.management_form }}
{% for form in worker_formset %}
{{ form }}
{% endfor %}
{% for form in goods_formset %}
{{ form }}
{% endfor %}
</form>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question