T
T
tosterru2015-03-05 12:18:52
Django
tosterru, 2015-03-05 12:18:52

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)

Those. we have a Warehouse that has a Manager, Workers and Goods.
I would like to make a page that displays:
1. Warehouse manager, next to him is the Change button, when clicked, you go to the page with possible candidates. Another Delete button, there may not be a boss in the warehouse.
2. Employees, which may be several. There are also Delete and Change buttons next to them. And also a drop-down list with possible positions (type from WorkerType).
3. Goods, the same as employees, but in addition to the type of goods, there is the Quantity of goods field.
I don't even know how to make a View. The only thing I could think of was to inherit from UpdateView, but there is a drop-down list for head, and a list with multiple selection for worker and goods.
Help, please, to understand.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anatoly Scherbakov, 2015-03-05
@tosterru

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.

T
tosterru, 2015-03-14
@tosterru

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>

If someone hears me, how correct is it to do so?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question