Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango CBV Forms prepopulated foreign key dataset
    primarykey
    data
    text
    <p>I'm trying to use CBVs as much as possible and want to pre-populate data in a ModelForm based on a generic.CreateView with some data passed in via URL.</p> <p>I might be over thinking or confusing myself. All code abridged for legibility</p> <p>We have an inventory system with PartNumbers (abstractions), Carriers (actual instances of PartNumbers with location, serial and quantity numbers) and Movements for recording when items are extracted from the inventory, how much is taken and what Carrier it came from. </p> <p>I would like to have the "extract inventory" link on the PartNumber detail page, and then have the available carriers ( pn.carrier_set.all() ) auto filled into the FK drop down on the MovementForm.</p> <p><strong>models.py</strong></p> <pre><code>class PartNumber(models.Model): name = models.CharField("Description", max_length=100) supplier_part_number = models.CharField(max_length=30, unique=True) slug = models.SlugField(max_length=40, unique=True) class Carrier(models.Model): part_numbers = models.ForeignKey(PartNumber) slug = models.SlugField(max_length=10, unique=True, blank=True, editable=False) location = models.ForeignKey(Location) serial_number = models.CharField(max_length=45, unique=True, null=True, blank=True) qty_at_new = models.IntegerField() qty_current = models.IntegerField() class Movement(models.Model): carrier = models.ForeignKey(Carrier) date = models.DateField(default=timezone.now()) qty = models.IntegerField() </code></pre> <p>I have been playing around with get_initial() and get_form_kwargs() without success:</p> <p>In <strong>urls.py</strong> I collect the PartNumber via url as pn_slug</p> <pre><code>url(r'^partnumber/(?P&lt;pn_slug&gt;[-\w]+)/extract/$', views.MovementCreate.as_view(), name='pn_extract'), </code></pre> <p><strong>forms.py</strong> is generic</p> <pre><code>class MovementForm(forms.ModelForm): class Meta: model = Movement </code></pre> <p><strong>views.py</strong> </p> <pre><code>class MovementCreate(generic.CreateView): form_class = MovementForm model = Movement def get_form_kwargs(self): kwargs = super(MovementCreate, self).get_form_kwargs() kwargs['pn_slug'] = self.request.POST.get("pn_slug") return kwargs # here we get the appropriate part and carrier and. # return it in the form def get_initial(self): initial = super(MovementCreate, self).get_initial() # this didn't work, hence using get_form_kwargs #pn = PartNumber.objects.get(slug=self.request.POST.get("pn_slug")) pn = PartNumber.objects.get(slug=self[pn_slug]) carriers = pn.carrier_set.all() initial['carrier'] = carriers return initial </code></pre> <p>As it stands, I'm getting "global name 'pn_slug' is not defined" errors - but I doubt that error accurately reflects what I have done wrong.</p> <p>I have been using these posts as rough guidelines:</p> <p><a href="https://stackoverflow.com/questions/5666505/how-to-subclass-djangos-generic-createview-with-initial-data">How to subclass django&#39;s generic CreateView with initial data?</a></p> <p><a href="https://stackoverflow.com/questions/5773724/how-do-i-use-createview-with-a-modelform">How do I use CreateView with a ModelForm</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload