Configure multi-field multi-select form in Django Admin

1,359 views
Skip to first unread message

rc

unread,
Nov 24, 2009, 12:13:58 PM11/24/09
to Django users
What I want to do is have a form with one field that allows you to
select a profile. Once this profile is selected there will be two
windows below that show available tests and selected tests. The Django
admin interface for the user permissions uses this format. Not sure
what it is called in Django.

My current models are listed below. I have a joining table called
Testsuite that joins tests to profiles. It works now, but the admin is
not very easy to add or remove tests to a testsuite since you have to
add one at a time. If someone can point me in the right direction I
would appreciate it.


Anyway I have the following models:
---------------------------------------------------
Models
--------------------------------------------------

class Profile(models.Model):
profile_id = models.AutoField(primary_key=True)
profile_name = models.CharField(max_length=75)
description = models.CharField(max_length=1500, blank=True)
def __unicode__(self):
return self.profile_name
class Meta:
db_table = u'profile'

class Testcase(models.Model):
test_id = models.AutoField(primary_key=True)
test_name = models.CharField(max_length=300)
src = models.ForeignKey(Source, null=True, blank=True)
bitrate = models.IntegerField(null=True, blank=True)
test_type = models.CharField(max_length=300)
output_resolution = models.CharField(max_length=15, blank=True)
description = models.CharField(max_length=3000, blank=True)
ref_file = models.CharField(max_length=765, blank=True)
ref_encode_filesize = models.IntegerField(null=True, blank=True)
ref_y_psnr = models.DecimalField(null=True, max_digits=7,
decimal_places=2, blank=True)
ref_u_psnr = models.DecimalField(null=True, max_digits=7,
decimal_places=2, blank=True)
ref_v_psnr = models.DecimalField(null=True, max_digits=7,
decimal_places=2, blank=True)
ref_yuv_psnr = models.DecimalField(null=True, max_digits=7,
decimal_places=2, blank=True)
highmark_version = models.CharField(max_length=60, blank=True)
def __unicode__(self):
return self.test_name
class Meta:
db_table = u'testcase'

class Testsuite(models.Model):
testsuite_id = models.AutoField(primary_key=True)
profile = models.ForeignKey(Profile, related_name='profile')
test = models.ForeignKey(Testcase, related_name='test')
#def __unicode__(self):
# return
self.testsuite_id
def __unicode__(self):
return '%s,%s' % (self.profile, self.test)
class Meta:
db_table = u'testsuite'

rc

unread,
Nov 24, 2009, 12:32:37 PM11/24/09
to Django users
As I mentioned, the Django admin has exactly what I want with the user
permission form (auth_user, auth_permissions and
auth_user_user_permission). Does anyone know where that code is
located in the Django environment?

Karen Tracey

unread,
Nov 24, 2009, 8:01:20 PM11/24/09
to django...@googlegroups.com
On Tue, Nov 24, 2009 at 12:32 PM, rc <reed...@gmail.com> wrote:
As I mentioned, the Django admin has exactly what I want with the user
permission form (auth_user, auth_permissions and
auth_user_user_permission). Does anyone know where that code is
located in the Django environment?


That's done with javascript, activated by listing the fields in:

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.filter_horizontal

The javascript will be found somewhere under django/contrib/admin/media/js.

Karen

Tim Valenta

unread,
Nov 24, 2009, 9:41:53 PM11/24/09
to Django users
> The javascript will be found somewhere under django/contrib/admin/media/js.

To be clear, rc, although the javascript is of course available for
viewing, you should only need to put that attribute
"filter_horizontal" on your ModelAdmin. There shouldn't be any need
for inserting any custom javascript commands anywhere. That one
attribute does it all.

And... this might pose a problem, but I don't see any ManyToManyFields
in your models. It won't work unless you're using those, instead of
ForeignKeys.

For the extra mile, you shouldn't need those various "xxxx_id =
models.AutoField(primary_key=True)" parts. Django does that
automatically, but names each one just "id", instead of "[modelname]
_id". Unless you specifically want them to be named differently than
"id", you don't have to specify it, which helps it be more readable.
But it's of course your own project :) Do it however you wish!

Tim

On Nov 24, 6:01 pm, Karen Tracey <kmtra...@gmail.com> wrote:
> On Tue, Nov 24, 2009 at 12:32 PM, rc <reedcr...@gmail.com> wrote:
> > As I mentioned, the Django admin has exactly what I want with the user
> > permission form (auth_user, auth_permissions and
> > auth_user_user_permission). Does anyone know where that code is
> > located in the Django environment?
>
> That's done with javascript, activated by listing the fields in:
>
> http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contri...

rc

unread,
Nov 25, 2009, 1:34:52 PM11/25/09
to Django users
Tim,

Thanks for the response. That is what I was looking for. I got it
working now, just needed a little hint. Found the User Admin stuff (in
contrib) and was able to follow that.
As far as the "id" columns.....I adapted Django to an existing system
so that is why the models don't necessarily match the Django norm.

Reed
Reply all
Reply to author
Forward
0 new messages