How to copy record with Detailview and picking up on pk (Django)

Asked

Viewed 284 times

1

Consider my template:

entry_detail.html

<form class="navbar-form navbar-right" action="." method="get">
    <!-- add -->
    <!-- <p name="filter_link" class="pull-right"><a href="">Produtos em baixo estoque</a></p> -->
    <a name="new_customer" href="{% url 'proposal_list' %}">
        <button type="button" class="btn btn-primary">
            <span class="glyphicon glyphicon-plus"></span> Criar Orçamento
        </button>
    </a>
</form>

And consider my views:

py views.

class EntryDetail(DetailView):
    template_name = 'core/entry/entry_detail.html'
    model = Entry

    def create_proposal(self, employee_pk=8):
        if 'new_customer' in self.request.GET:
            employee = Employee.objects.get(pk=employee_pk)
            num_last_proposal = NumLastProposal.objects.get(
                pk=1)  # sempre pk=1
            entry = Entry.objects.get(pk=self.request.GET[self.id])
            obj = Proposal(
                num_prop=num_last_proposal.num_last_prop + 1,
                type_prop='R',
                category=entry.category,
                description=entry.description,
                work=entry.work,
                person=entry.person,
                employee=employee,
                seller=entry.seller,
            )
            obj.save()

            entry.is_entry = True
            entry.save()

            num_last_proposal.num_last_prop += 1
            num_last_proposal.save()

Question: How do I make it work? I need to get the entry.pk from the page I’m on, in this case, entry.pk, I’m on this page because I’m on Detailview.

entry = Entry.objects.get(pk=self.request.GET[self.id])

Using Manage.py shell works, but in the template I need to choose Entry, ie, get the pk.

# shell_create_proposal.py

from core.models import Entry, Proposal, Employee, NumLastProposal

employee = Employee.objects.get(pk=8)
num_last_proposal = NumLastProposal.objects.get(pk=1)
entry = Entry.objects.get(pk=1)
obj = Proposal(
    num_prop=num_last_proposal.num_last_prop + 1,
    type_prop='R',
    category=entry.category,
    description=entry.description,
    work=entry.work,
    person=entry.person,
    employee=employee,
    seller=entry.seller,
)
obj.save()

entry.is_entry = True
entry.save()

num_last_proposal.num_last_prop = num_last_proposal.num_last_prop + 1
num_last_proposal.save()

1 answer

0

Given what you have exposed, I believe your url is something like site.com/entry/123. So your.py urls should be set to something like:

 url(r'^entry/(?P<entry_id>\d+)/$', entry, name='entry'),

So your code would be something like:

def create_proposal(self, employee_pk=8, **kwargs):
    # (...)
    entry = Entry.objects.get(pk=kwargs.get('entry_id',None))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.