How to copy register with DetailView and get pk (Django) -
consider 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 considerer view:
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 make work this. how entry.pk in detailview use in
entry = entry.objects.get(pk=self.request.get[self.id])
in manage.py shell work
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()
specify pk in url , detail view should handle you
url(r'^yourview/(?p<pk>\d+)/' , yourdetailview.as_view() , name='your_view_name' ),
look @ get_object method here:
http://ccbv.co.uk/projects/django/1.8/django.views.generic.detail/detailview/
it gets pk follows:
pk = self.kwargs.get(self.pk_url_kwarg, none)
Comments
Post a Comment