How to Make a Custom Inline Form for Admin

2018-04-17 Thread Mark Phillips
I am using django 1.11. I have these models (among others):

models.py:# MetaDataclass 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.


Re: Custom inline form

2008-12-02 Thread mrsource

I find the problem:
The validation was performed also on new extra widget because I have
added a field that was always valorized then the self.has_changed
property was always true then the validation was performed also for
the new extra widget. Now the field is empty by default.

But now another problem, I would override the save_model method for
the inline model to read the value of my custom widget. The problem is
that if I put the save_model in ModelInline or in ModelAdmin class it
is never executed...How can I read the property from my custom widget?


mrsource ha scritto:

> In the inline model option I have overriden the inline form with a
> custom form where I only added a choices widget, now the validation
> has a wrong behaviour...more precisely if I have set extra property to
> 2 in inline model options, when I click "Save" django try to validate
> even the two empty extra inline model that I have left blank and show
> an error like "the primary key field must not be empty"... If I remove
> my custom form all is ok.
>
> How can I avoid this validation on the empty extra filed?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Custom inline form

2008-12-01 Thread mrsource


In the inline model option I have overriden the inline form with a
custom form where I only added a choices widget, now the validation
has a wrong behaviour...more precisely if I have set extra property to
2 in inline model options, when I click "Save" django try to validate
even the two empty extra inline model that I have left blank and show
an error like "the primary key field must not be empty"... If I remove
my custom form all is ok.

How can I avoid this validation on the empty extra filed?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---