Issue
I have the following two Django models:
class Item(models.Model):
description = models.CharField(max_length=250)
total_quantity = models.PositiveSmallIntegerField(default=0)
def get_absolute_url(self):
return reverse_lazy('inventory-item-details', kwargs={'pk': self.pk})
class PurchaseEntry(models.Model):
item = models.ForeignKey(Item, on_delete=models.CASCADE)
...
I am displaying all of the Item objects I have inside a table.
I added a link next to each row item. The link is labeled "Create a Purchase Entry", when opening the link it should display a form for creating PurchaseEntry objects (I'm going to use a generic CreateView for this).
Everything is easy with the exception of one key part.
When opening the form, I want the Item to be prepopulated with the one I just clicked on its link, so the user won't have to select from a list of items again.
The only thing that came to mind was to include the Primary Key when navigating into the form, and use that primary key to select one of the options. But I honestly don't know how to implement this, it's just an idea at the moment, maybe you have a better one?
Just for context, and in order for you to help me better here's some more code: My views file:
class ItemListView(LoginRequiredMixin, generic.ListView):
template_name = 'inventory/home.html'
context_object_name = 'items'
model = Item
class ItemDetailsView(LoginRequiredMixin, generic.DetailView):
template_name = 'inventory/item-details.html'
context_object_name = 'item'
model = Item
class ItemCreateView(LoginRequiredMixin, generic.CreateView):
template_name = 'inventory/add-item.html'
model = Item
fields = '__all__'
success_url = reverse_lazy('inventory')
class ItemDeleteView(LoginRequiredMixin, generic.DeleteView):
model = Item
success_url = reverse_lazy('inventory')
My urls file:
urlpatterns = [
path('', views.ItemListView.as_view(), name='inventory'),
path('items/add/', views.ItemCreateView.as_view(), name='inventory-item-add'),
path('items/<int:pk>/', views.ItemDetailsView.as_view(), name='inventory-item-details'),
path('items/<int:pk>/delete', views.ItemDeleteView.as_view(), name='inventory-item-delete'),
]
Solution
pass the foreignkey to your url:
path("purchase-entry/add/<int:pk>/....)
Then in your view, you can override get_initial()
method to repopulate the item field:
class PurchaseCreateView(CreateView):
...
def get_initial(self):
item = self.get_object()
return {" item": item.pk}
Answered By - McPherson
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.