I am using django 1.11. I have these models (among others):

models.py:# MetaData        class MetaData(models.Model):
    metadata_id = models.AutoField(primary_key = True)
    name = models.CharField('metadata name', max_length=200, unique=True)
    description = models.TextField('description')

    def __str__(self):
        return self.name
# MetaData Value                   class MetaDataValue(models.Model):
    metadata_id = models.ForeignKey(MetaData, on_delete=models.CASCADE,)
    value = models.CharField('value', max_length=200, unique=True)

    def __str__(self):
        return self.value
# Document               class Document(models.Model):
    document_id = models.AutoField(primary_key=True)
    title = models.CharField('title', max_length=200)
    description = models.TextField('description')
    created = models.DateTimeField(editable=False)
    updated = models.DateTimeField(editable=False)
    storage_file_name = models.FileField('File name',
upload_to=unique_file_path)
    metadata = models.ManyToManyField('MetaData',
through='DocumentMetaDataValue', through_fields=('document',
'metadata'))
    metadatavalue = models.ManyToManyField('MetaDataValue',
through='DocumentMetaDataValue', through_fields=('document',
'metadataValue'))
class DocumentMetaDataValue(models.Model):
    document = models.ForeignKey(Document, on_delete=models.CASCADE)
    metadata = models.ForeignKey(MetaData, on_delete=models.CASCADE)
    metadataValue = models.ForeignKey(MetaDataValue, on_delete=models.CASCADE)

admin.py:class Fred(forms.ModelForm):
    """ something newer
    """
    class Meta:
        model = DocumentMetaDataValue
        fields = '__all__'
class DocumentMetaDataValueInline(admin.TabularInline):
    model = DocumentMetaDataValue
    form = Fred
class DocumentAdmin(admin.ModelAdmin):
    form = DocumentForm
    list_display = ('get_document_type', 'document_state', 'title',
'description', 'original_file_name', 'created', 'storage_file_name',
'get_thumb', )
    ordering = ('created',)
    readonly_field = ('document_state',)
    filter_horizontal = ('metadata', 'metadatavalue', )
    inlines = (DocumentMetaDataValueInline, )
    # other code not related to this problem

admin.site.register(Document, DocumentAdmin)

As written above, the inline form Fred in the DocumentAdmin page shows two
select boxes side by side - one for metadata names and one for metadata
values (the other Document fields are also presented). The problem is that
all the metadata names are in the first select box, and all the metadata
values are in the second select box. The relationship between metadata
names and values is lost.

For example, there is a metadata name 'Person' with 12 associated values
(Peter, Sam, Sally, etc.). Another metadata name is 'Decade', with multiple
values (1890, 1900, 1910, etc). The first select box has both Person and
Decade, while the second select box has all thes value - (Peter, Sam,
Sally, ...1890, 1900, 1910....). Picking the correct metadata value for a
particular metadata name in the current implementation is fraught with
potential errors. I could write a ton of validation code to fix this
problem, but I think a different inline form would work better.

I would like the new Fred inline form to have three elements - a check box,
the metadata name, and a select box with only the metadata values for that
name. All the metadata names would be shown in this inline form. It would
be something like this

[ ]    Person      {select box with Peter, Sally, Sam, etc}
[ ]    Decade     {select box with 1890, 1900, 1910, etc}

A user would click a check box for Person and then select an option from
the associated select box to add that metadata information to the
DocumentMetaDataValue table.

I have been playing around with using forms. ModelForms and forms.Forms,
and can't seem to get anything to work.

Is it possible to do what I want to do? How can I build this custom inline
form? Is there a better way?

Thanks!

Mark

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAEqej2N3zKJsYmu%3DYRqgdgkB%2BBZkdhFP3u8nCWNZh%3DxGvwAYVg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to