Multiple ModelAdmins for a single Model
Hello, I have a table, which contains three types of page. Each page has its own type of information. Up to now, all possible fields from the table have been displayed on an admin form and a drop down box provides the option to determine the type of page (page_type). Validation is carried out per the option selected. What I'd like to do, to simplify the interface for end users, is provide three separate ModelAdmins for each page type, so in the Django admin site I'd like to have "Web Site Pages", "Video Pages", "Message Pages", as apposed to the current one-for-all "Pages" option. Obviously each of the three ModelAdmins should only allow for manipulating data (in the Pages model) that is relevant to to the given 'page_type'. I've tried creating two ModelAdmins and registering each for the same Model, but that throws an AlreadyRegistered exception, plus I can't see that way how I'd filter data to a specific 'page_type' in the Model. Any help would be greatly appreciated. Thanks, Dan Ward. --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Multiple ModelAdmins for a single Model
On Sat, 2009-03-21 at 03:31 -0700, Dan Ward wrote: [...] > What I'd like to do, to simplify the interface for end users, is > provide three separate ModelAdmins for each page type, so in the > Django admin site I'd like to have "Web Site Pages", "Video Pages", > "Message Pages", as apposed to the current one-for-all "Pages" > option. > Obviously each of the three ModelAdmins should only allow for > manipulating data (in the Pages model) that is relevant to to the > given 'page_type'. > > I've tried creating two ModelAdmins and registering each for the same > Model, but that throws an AlreadyRegistered exception, plus I can't > see that way how I'd filter data to a specific 'page_type' in the > Model. You can only have one ModelAdmin registered per model per admin site. ModelAdmin is the class that defines *the* admin interface for that model with that site, so the error is reasonable. That's the bad news. The semi-good news is that you're probably thinking about this at the wrong level of abstraction. You're wanting to do something a bit fiddly, so it's not going to be too surprising that you have to roll up your sleeves a bit here. I suspect what you want is possible, though, with a little code reading and work (without being fragile). You seem to be saying you really want a different form displayed for each type of page. Presumably it's easy to tell which type of form should be displayed -- at least, you can work it out from the incoming request. That term "form" is the clue. Inside the ModelAdmin class, there's some code (we can guess this without even reading the implementation) that constructs the form to display. Fortunately, that code is in its own method, begging you to override it. Have a look at ModelAdmin.get_form() -- it seems to be exactly the thing you want to override. So, in your ModelAdmin subclass, you write your own get_form() method that uses the request to return the right type of ModelForm for your situation. I think that should completely solve your problem. If not, come back with questions and some explanation of what isn't working or how I've entirely misunderstood your problem. Regards, Malcolm --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Multiple ModelAdmins for a single Model
On Sat, Mar 21, 2009 at 5:58 PM, Malcolm Tredinnick < malc...@pointy-stick.com> wrote: > > On Sat, 2009-03-21 at 03:31 -0700, Dan Ward wrote: > [...] > > What I'd like to do, to simplify the interface for end users, is > > provide three separate ModelAdmins for each page type, so in the > > Django admin site I'd like to have "Web Site Pages", "Video Pages", > > "Message Pages", as apposed to the current one-for-all "Pages" > > option. > > Obviously each of the three ModelAdmins should only allow for > > manipulating data (in the Pages model) that is relevant to to the > > given 'page_type'. > > > > I've tried creating two ModelAdmins and registering each for the same > > Model, but that throws an AlreadyRegistered exception, plus I can't > > see that way how I'd filter data to a specific 'page_type' in the > > Model. > > You can only have one ModelAdmin registered per model per admin site. > ModelAdmin is the class that defines *the* admin interface for that > model with that site, so the error is reasonable. > > That's the bad news. The semi-good news is that you're probably thinking > about this at the wrong level of abstraction. You're wanting to do > something a bit fiddly, so it's not going to be too surprising that you > have to roll up your sleeves a bit here. I suspect what you want is > possible, though, with a little code reading and work (without being > fragile). > > You seem to be saying you really want a different form displayed for > each type of page. Presumably it's easy to tell which type of form > should be displayed -- at least, you can work it out from the incoming > request. > > That term "form" is the clue. Inside the ModelAdmin class, there's some > code (we can guess this without even reading the implementation) that > constructs the form to display. Fortunately, that code is in its own > method, begging you to override it. Have a look at ModelAdmin.get_form() > -- it seems to be exactly the thing you want to override. > > So, in your ModelAdmin subclass, you write your own get_form() method > that uses the request to return the right type of ModelForm for your > situation. I think that should completely solve your problem. > > If not, come back with questions and some explanation of what isn't > working or how I've entirely misunderstood your problem. > > Regards, > Malcolm > > > > > In the development version there's actually a more clever solution to this. You can create pure python subclasses(proxy classes) that use the same DB table and fields and everything, but have their own class. So you could create the relevant proxy models, and register the ModelAdmins for each one of these proxy models. Alex -- "I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire "The people's good is the highest law."--Cicero --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Multiple ModelAdmins for a single Model
On Sat, 2009-03-21 at 18:01 -0400, Alex Gaynor wrote: [...] > In the development version there's actually a more clever solution to > this. You can create pure python subclasses(proxy classes) that use > the same DB table and fields and everything, but have their own class. > So you could create the relevant proxy models, and register the > ModelAdmins for each one of these proxy models. Only if the original poster wants three separate admin entries on the index page. It depends on the requirements, which hopefully the original poster will explain. Regards, Malcolm --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Multiple ModelAdmins for a single Model
Hi, First of all thanks for the replies. I won't be able to try anything until tomorrow, however it has given some food for thought. Malcom, the three admin entries is what I'm looking for. Essentially, I'm aiming to have the admin home page display the pages block as so: Pages (admin group) -- Rotation (rotation model) -- Web Page (page model) -- Video Page (page model) -- Message Page (page model) The rotation page really doesn't matter. I'll look in to improvements at a later stage, however the focus is on having three separate admin forms for a single model. Really, looking back on it, it would have been better in multiple models, but that's besides the point, I don't have the time to go back on what I've done there. I hope this clears things up a bit. On 21 Mar, 22:04, Malcolm Tredinnick wrote: > On Sat, 2009-03-21 at 18:01 -0400, Alex Gaynor wrote: > > [...] > > > In the development version there's actually a more clever solution to > > this. You can create pure python subclasses(proxy classes) that use > > the same DB table and fields and everything, but have their own class. > > So you could create the relevant proxy models, and register the > > ModelAdmins for each one of these proxy models. > > Only if the original poster wants three separate admin entries on the > index page. It depends on the requirements, which hopefully the original > poster will explain. > > Regards, > Malcolm --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Multiple ModelAdmins for a single Model
To follow up, I can see how I can achieve different forms, however I can't figure out how to provide three separate links in the admin index page. Any suggestions? On Mar 22, 11:22 am, Dan Ward wrote: > Hi, > > First of all thanks for the replies. I won't be able to try anything > until tomorrow, however it has given some food for thought. > > Malcom, the three admin entries is what I'm looking for. Essentially, > I'm aiming to have the admin home page display the pages block as so: > > Pages (admin group) > -- Rotation (rotation model) > -- Web Page (page model) > -- Video Page (page model) > -- Message Page (page model) > > The rotation page really doesn't matter. I'll look in to improvements > at a later stage, however the focus is on having three separate admin > forms for a single model. Really, looking back on it, it would have > been better in multiple models, but that's besides the point, I don't > have the time to go back on what I've done there. > > I hope this clears things up a bit. > > On 21 Mar, 22:04, Malcolm Tredinnick wrote: > > > On Sat, 2009-03-21 at 18:01 -0400, Alex Gaynor wrote: > > > [...] > > > > In the development version there's actually a more clever solution to > > > this. You can create pure python subclasses(proxy classes) that use > > > the same DB table and fields and everything, but have their own class. > > > So you could create the relevant proxy models, and register the > > > ModelAdmins for each one of these proxy models. > > > Only if the original poster wants three separate admin entries on the > > index page. It depends on the requirements, which hopefully the original > > poster will explain. > > > Regards, > > Malcolm > > --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Multiple ModelAdmins for a single Model
OK, I've done a fair amount of research and I want to detail what I'm aiming to achieve. I have a page table. The page table contains various fields, which are dependent upon the pgtype option (containing three possible values). What I want to achieve is three separate pages in the admin site, which show only their specified pgtype's options. Each admin page should appear as it's own Model, seemly unrelated. When visiting a 'Video' page, for example from the admin site, it should only list the video pages from the pages table. When adding a new item, it should only show only relevant 'Video' fields. This should also be the case for 'Message' and 'Website' pages (the other two pgtype values). I really don't want to start going down the route of model redesign to have separate tables. Especially as the database is already in use. Thanks. On Mar 24, 11:57 am, Dan Ward wrote: > To follow up, > > I can see how I can achieve different forms, however I can't figure > out how to provide three separate links in the admin index page. > > Any suggestions? > > On Mar 22, 11:22 am, Dan Ward wrote: > > > Hi, > > > First of all thanks for the replies. I won't be able to try anything > > until tomorrow, however it has given some food for thought. > > > Malcom, the three admin entries is what I'm looking for. Essentially, > > I'm aiming to have the admin home page display the pages block as so: > > > Pages (admin group) > > -- Rotation (rotation model) > > -- Web Page (page model) > > -- Video Page (page model) > > -- Message Page (page model) > > > The rotation page really doesn't matter. I'll look in to improvements > > at a later stage, however the focus is on having three separate admin > > forms for a single model. Really, looking back on it, it would have > > been better in multiple models, but that's besides the point, I don't > > have the time to go back on what I've done there. > > > I hope this clears things up a bit. > > > On 21 Mar, 22:04, Malcolm Tredinnick wrote: > > > > On Sat, 2009-03-21 at 18:01 -0400, Alex Gaynor wrote: > > > > [...] > > > > > In the development version there's actually a more clever solution to > > > > this. You can create pure python subclasses(proxy classes) that use > > > > the same DB table and fields and everything, but have their own class. > > > > So you could create the relevant proxy models, and register the > > > > ModelAdmins for each one of these proxy models. > > > > Only if the original poster wants three separate admin entries on the > > > index page. It depends on the requirements, which hopefully the original > > > poster will explain. > > > > Regards, > > > Malcolm > > --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Multiple ModelAdmins for a single Model
Did the proxy table solution that Alex suggested work out for you? That's the one I try just as soon as I upgrade to trunk (some of the admin template changes in trunk breaks my admin template overrides so I might hold off for the 1.1 release in case it changes some more) On Mar 24, 2:53 pm, Dan Ward wrote: > OK, > > I've done a fair amount of research and I want to detail what I'm > aiming to achieve. > > I have a page table. The page table contains various fields, which are > dependent upon the pgtype option (containing three possible values). > > What I want to achieve is three separate pages in the admin site, > which show only their specified pgtype's options. Each admin page > should appear as it's own Model, seemly unrelated. > > When visiting a 'Video' page, for example from the admin site, it > should only list the video pages from the pages table. When adding a > new item, it should only show only relevant 'Video' fields. This > should also be the case for 'Message' and 'Website' pages (the other > two pgtype values). > > I really don't want to start going down the route of model redesign to > have separate tables. Especially as the database is already in use. > > Thanks. > > On Mar 24, 11:57 am, Dan Ward wrote: > > > To follow up, > > > I can see how I can achieve different forms, however I can't figure > > out how to provide three separate links in the admin index page. > > > Any suggestions? > > > On Mar 22, 11:22 am, Dan Ward wrote: > > > > Hi, > > > > First of all thanks for the replies. I won't be able to try anything > > > until tomorrow, however it has given some food for thought. > > > > Malcom, the three admin entries is what I'm looking for. Essentially, > > > I'm aiming to have the admin home page display the pages block as so: > > > > Pages (admin group) > > > -- Rotation (rotation model) > > > -- Web Page (page model) > > > -- Video Page (page model) > > > -- Message Page (page model) > > > > The rotation page really doesn't matter. I'll look in to improvements > > > at a later stage, however the focus is on having three separate admin > > > forms for a single model. Really, looking back on it, it would have > > > been better in multiple models, but that's besides the point, I don't > > > have the time to go back on what I've done there. > > > > I hope this clears things up a bit. > > > > On 21 Mar, 22:04, Malcolm Tredinnick wrote: > > > > > On Sat, 2009-03-21 at 18:01 -0400, Alex Gaynor wrote: > > > > > [...] > > > > > > In the development version there's actually a more clever solution to > > > > > this. You can create pure python subclasses(proxy classes) that use > > > > > the same DB table and fields and everything, but have their own class. > > > > > So you could create the relevant proxy models, and register the > > > > > ModelAdmins for each one of these proxy models. > > > > > Only if the original poster wants three separate admin entries on the > > > > index page. It depends on the requirements, which hopefully the original > > > > poster will explain. > > > > > Regards, > > > > Malcolm --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Multiple ModelAdmins for a single Model
I tried a number of ways, but still failed to get where I needed. I've created my own control panel now. On Mar 27, 1:18 pm, andybak wrote: > Did the proxy table solution that Alex suggested work out for you? > > That's the one I try just as soon as I upgrade to trunk (some of the > admin template changes in trunk breaks my admin template overrides so > I might hold off for the 1.1 release in case it changes some more) > > On Mar 24, 2:53 pm, Dan Ward wrote: > > > OK, > > > I've done a fair amount of research and I want to detail what I'm > > aiming to achieve. > > > I have a page table. The page table contains various fields, which are > > dependent upon the pgtype option (containing three possible values). > > > What I want to achieve is three separate pages in the admin site, > > which show only their specified pgtype's options. Each admin page > > should appear as it's own Model, seemly unrelated. > > > When visiting a 'Video' page, for example from the admin site, it > > should only list the video pages from the pages table. When adding a > > new item, it should only show only relevant 'Video' fields. This > > should also be the case for 'Message' and 'Website' pages (the other > > two pgtype values). > > > I really don't want to start going down the route of model redesign to > > have separate tables. Especially as the database is already in use. > > > Thanks. > > > On Mar 24, 11:57 am, Dan Ward wrote: > > > > To follow up, > > > > I can see how I can achieve different forms, however I can't figure > > > out how to provide three separate links in the admin index page. > > > > Any suggestions? > > > > On Mar 22, 11:22 am, Dan Ward wrote: > > > > > Hi, > > > > > First of all thanks for the replies. I won't be able to try anything > > > > until tomorrow, however it has given some food for thought. > > > > > Malcom, the three admin entries is what I'm looking for. Essentially, > > > > I'm aiming to have the admin home page display the pages block as so: > > > > > Pages (admin group) > > > > -- Rotation (rotation model) > > > > -- Web Page (page model) > > > > -- Video Page (page model) > > > > -- Message Page (page model) > > > > > The rotation page really doesn't matter. I'll look in to improvements > > > > at a later stage, however the focus is on having three separate admin > > > > forms for a single model. Really, looking back on it, it would have > > > > been better in multiple models, but that's besides the point, I don't > > > > have the time to go back on what I've done there. > > > > > I hope this clears things up a bit. > > > > > On 21 Mar, 22:04, Malcolm Tredinnick wrote: > > > > > > On Sat, 2009-03-21 at 18:01 -0400, Alex Gaynor wrote: > > > > > > [...] > > > > > > > In the development version there's actually a more clever solution > > > > > > to > > > > > > this. You can create pure python subclasses(proxy classes) that use > > > > > > the same DB table and fields and everything, but have their own > > > > > > class. > > > > > > So you could create the relevant proxy models, and register the > > > > > > ModelAdmins for each one of these proxy models. > > > > > > Only if the original poster wants three separate admin entries on the > > > > > index page. It depends on the requirements, which hopefully the > > > > > original > > > > > poster will explain. > > > > > > Regards, > > > > > Malcolm --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---