In Django, what is the better way to pass arguments to a create view?

  backend, django, python

I need help with two questions.


Number one: I have Orders model and generic create view for it and OrderDetails model with create view. After order is created the get_absolute_url method in Orders model redirects to a page with OrderDetails creation form. I want this OrderDetails create view to get arguments from order we just created such as order.pk, now I have to fill this field manually. So, is get_absolute_url method is what I need, or should I override dispatch method in my view, but may be I need to work on get_success_url method?

Number two: when I go to Order creation page, how to automatically fill Employee field? E.g. I’m logged in system as Bob Smith and when I create new order I don’t want to set myself there manually, I want system do it for me.

Here’s models:

class OrderDetails(models.Model):
    order = models.OneToOneField('Orders', models.DO_NOTHING, primary_key=True)
    product = models.ForeignKey('products.Products', models.DO_NOTHING)
    unit_price = models.FloatField()
    quantity = models.SmallIntegerField()
    discount = models.FloatField()

    class Meta:
        managed = False
        db_table = 'order_details'
        unique_together = (('order', 'product'),)


class Orders(models.Model):
    customer = models.ForeignKey('contractors.Customers', models.DO_NOTHING, blank=True, null=True)
    employee = models.ForeignKey('employees.Employees', models.DO_NOTHING, blank=True, null=True)
    order_date = models.DateField(blank=True, null=True)
    required_date = models.DateField(blank=True, null=True)
    shipped_date = models.DateField(blank=True, null=True)
    ship_via = models.ForeignKey('contractors.Shippers', models.DO_NOTHING, db_column='ship_via', blank=True, null=True)
    freight = models.FloatField(blank=True, null=True)
    ship_name = models.CharField(max_length=40, blank=True, null=True)
    ship_address = models.CharField(max_length=60, blank=True, null=True)
    ship_city = models.CharField(max_length=15, blank=True, null=True)
    ship_region = models.CharField(max_length=15, blank=True, null=True)
    ship_postal_code = models.CharField(max_length=10, blank=True, null=True)
    ship_country = models.CharField(max_length=15, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'orders'

    def get_absolute_url(self):
        return u'/orders/%d/details' % self.pk

    def __str__(self):
        return f"{self.customer} {self.order_date}"

Views:

class OrdersCreateView(LoginRequiredMixin, CreateView):
    model = Orders
    template_name = 'orders/orders_create.html'
    form_class = OrderCreateOrUpdateForm

    def form_valid(self, form):
        return super().form_valid(form)

class OrderDetailsCreateView(LoginRequiredMixin, CreateView):
    model = OrderDetails
    template_name = 'orders/orderdetails_create.html'
    form_class = OrderDetailCreateOrUpdateForm

    def form_valid(self, form):
        return super().form_valid(form)

Forms:

class OrderCreateOrUpdateForm(forms.ModelForm):

    class Meta:
        model = Orders
        fields = [
            'customer', 'employee', 'order_date', 'required_date',
            'shipped_date', 'ship_via', 'freight', 'ship_name', 'ship_address',
            'ship_city', 'ship_region', 'ship_postal_code', 'ship_country']
        widgets = {
            'order_date': DateInput(),
            'required_date': DateInput(),
            'shipped_date': DateInput(),
        }


class OrderDetailCreateOrUpdateForm(forms.ModelForm):

    class Meta:
        model = OrderDetails
        fields = ['order', 'product', 'unit_price', 'quantity', 'discount']

Templates:

 {% extends 'home/base.html' %}
    {% block content %}
    <h2>Order <p>{{ object.customer }}</p> <p>{{ object.order_date }}</p></h2>
    <p>{{ object.employee }}</p> <p>{{object.required_date}}</p> <p>{{object.shipped_date}}</p>
    <p>{{object.ship_via}}</p> <p>{{ object.freight }}</p> <p>{{ship_name}}</p> <p>{{ship_address}}</p>
    <p>{{ ship_city}}</p> <p>{{ship_region}}</p> <p>{{ship_postal_code}}</p> <p>{{ship_country}}</p>
    <a href="{{ object.pk }}/update">Change order data</a>
    {% endblock %}

{% extends 'home/base.html' %}
{% load static %}
{% load crispy_forms_tags %}
{% block content %}
    <div>
        <form method='POST'>
            {% csrf_token %}
            <fieldset class='form-group'>
                <legend class='border-bottom mb-4'>
                    Add order details</legend>
                {{ form|crispy }}
            </fieldset>
            <div class='form-group'>
                <button class="btn btn-outline-info" type=
                "submit">Send</button>
            </div>
        </form>
    </div>
{% endblock content %}

Source: Python Questions

LEAVE A COMMENT