How to copy record with DetailView and get in pk (Django)

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:

views.py

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 this work? I need to get the entry.pk of the page I am, in this case, entry.pk, I am on this page because I am in 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, that is, get 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()
    
asked by anonymous 11.07.2015 / 08:35

1 answer

0

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

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

Then your code would look something like:

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