0

I would like to implement a choicebox where the user can select the appropriate options and 'transfer' them to a 'selected box' before saving. Django has this for the admin interface , like in this post, but how to implement it in a normal website? Any links to relevent tutorials or an exampled based on te following would be welcome.

Here are my models for managed tables of the many2many relationship.

# models.py
class Container(models.Model):
    container_id = models.AutoField(primary_key=True)
    location_id = models.ForeignKey(Location, db_column='location_id', on_delete = models.PROTECT)
    icon_desc = models.ForeignKey(Icon, db_column='icon_desc', null=True, blank=True, default='Box',on_delete = models.PROTECT)
    container_name = models.CharField(max_length=50, blank=True, null=True)

class Sample(models.Model):
    sample_id = models.AutoField(primary_key=True)
    containers = models.ManyToManyField(Container, through='JoinSampleContainer', through_fields=('sample_id', 'container_id'), related_name='sample')

class JoinSampleContainer(models.Model):
    id = models.AutoField(primary_key=True)
    sample_number = models.IntegerField()
    container_id = models.ForeignKey(Container, db_column='container_id', on_delete = models.PROTECT)
    sample_id = models.ForeignKey(Sample, db_column='sample_id', on_delete = models.PROTECT)

In my Views.py I have for a normal form setup:

def assignsample(request):
    if request.method == "POST":
        form = AssignForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.save()
            return redirect('alldepotsamples')
    else:
        form = AssignForm()
    return render(request, 'depotsample/assignsample.html', {'form': form})

And the form:

class AssignForm(forms.ModelForm):
    class Meta:
        model = Sample
        fields = (
        'sample_id',
        'containers'
        )

This is presented in the html using the placeholders:

{{ form.sample_id}}
{{ form.containers}}
Spatial Digger
  • 1,883
  • 1
  • 19
  • 37

0 Answers0