Discussion:
Custom Admin Form for ManyToMany, missing Green Plus Sign?
john
2010-02-13 23:30:03 UTC
Permalink
Hi, I just had fun creating a new Custom Admin Form to sort a
ManyToMany field by ABC order (why isnt it in ABC order by default on
the "def __unicode__" item?) It works great, but now the Green Plus
Sign to add more items to the list is missing, where did it go? I
guess its not enabled by default?

Does anyone know where I can search on this more, and/or what this
Green Plus Sign is called? Is this just an extra command that needs
to be entered or do I have to reinvent the wheel to get this back to
working?

thanks!


This is the Green Plus Sign im looking for (even though this is for a
ForeignKey field): Loading Image...


My code if you want to take a look (sorry that a lot of it has to be
redacted):

# more xyz/admin.py
from abc.xyz.models import Items,Stuff
from django.contrib import admin
from django import forms

class StuffForm(forms.ModelForm):

item=forms.ModelMultipleChoiceField(queryset=Items.objects.order_by('item'))

class Meta:
model=Stuff

class StuffAdmin(admin.ModelAdmin):
exclude=('items',)
form=StuffForm

admin.site.register(Stuff, StuffAdmin)


# more xyz/models.py
class Items(models.Model):
item=models.CharField(max_length=100)

def __unicode__(self):
return self.item

class Stuff(models.Model):
items=models.ManyToManyField(Items)
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Matt Schinckel
2010-02-14 04:11:24 UTC
Permalink
Post by john
Hi, I just had fun creating a new Custom Admin Form to sort a
ManyToMany field by ABC order (why isnt it in ABC order by default on
the "def __unicode__" item?)  It works great, but now the Green Plus
Sign to add more items to the list is missing, where did it go?  I
guess its not enabled by default?
Does anyone know where I can search on this more, and/or what this
Green Plus Sign is called?  Is this just an extra command that needs
to be entered or do I have to reinvent the wheel to get this back to
working?
thanks!
This is the Green Plus Sign im looking for (even though this is for a
ForeignKey field):http://docs.djangoproject.com/en/dev/_images/admin10.png
My code if you want to take a look (sorry that a lot of it has to be
# more xyz/admin.py
from abc.xyz.models import Items,Stuff
from django.contrib import admin
from django import forms
item=forms.ModelMultipleChoiceField(queryset=Items.objects.order_by('item'))
                model=Stuff
        exclude=('items',)
        form=StuffForm
admin.site.register(Stuff, StuffAdmin)
# more xyz/models.py
        item=models.CharField(max_length=100)
                return self.item
        items=models.ManyToManyField(Items)
I found it rather hard to follow your code, but I think the problem
might be that you haven't registered the object that is missing the
'green plus sign' with the admin. Without it, you cannot create new
objects of that type. You can access existing objects from other
models, however, which provides the behaviour you describe.

Matt.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
john
2010-02-14 05:49:59 UTC
Permalink
Maybe I didnt fully explain the issue, and it seems that Google added
a couple of extra lines to the code above.

Ive got an app with 2 Model classes.
The first one describes a product (the Item model above).
The second one (the Stuff model above), along with other data
references the Product via a ManyToMany key.
The basic default Admin settings, which work correctly, allow the
addition of new Products via the Green Plus Sign and a Pop-up Window.
Everything works fine.
However when adding new Products, the Admin interface sorts the
ManyToMany field by ID, instead of a much more intelligible Product
Name. Lots of Products, too hard to find a particular one in the
list.
Thus I wrote up a Custom Admin Form to sort these Products in the
ManyToMany field.
This works great, the Products are sorted in ABC order.
But, with this Custom Admin Form, the ability to Add another Product
via the Little Green Plus Sign has been removed, the Plus Sign just
isnt there, and therefore cannot spawn the Pop-up Window to add
another Product to the Items model DB table.
It seems that this should be an easy fix, but I cant find any docs
that dont involve reinventing the wheel.

Yes, the Items model data can be accessed through another part of the
Admin interface, but I think the purpose of the Green Plus Sign was to
alleviate this extra step.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Matt Schinckel
2010-02-16 12:18:09 UTC
Permalink
Post by john
Yes, the Items model data can be accessed through another part of the
Admin interface, but I think the purpose of the Green Plus Sign was to
alleviate this extra step.
Try registering the model of that type with the admin. The green plus
will only appear if the model is registered - otherwise it is unable
to find a change_form to allow for adding/editing the initial values.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Loading...