Wednesday, September 15, 2010

Re: Prepopulating fields from parent, or related models, in the admin

well...when you say inheritance...I was thinking that you were going to create some models like this:

class BaseProject(models.Model):
    title = models.CharField()
    field2 = models.IntegerField()
    field3 = models.TextField()

class SubProject(BaseProject)
    """
    inherits the fields from BaseProject and allows you to override and add custom fields
    """
    field3 = models.CharField()
    new_field = models.BigIntegerField()

Then once you've set up your admins for these...
you'll have two models you can interact with...one will be called BaseProject
and the other will be called SubProject...

These models in the admin don't inherit the data stored in them...they only inherit the fields defined...

SO long story short...

Inheritance won't transfer data from one model to another, it'll just allow you to inherit fields so if you had a model that was going to be 99% the same as a previous model, you'd just inherit from that, and then extend/override the field descriptions in the new model.

Umm yeah you could use Jquery to do that...it's one way..though the only thing I've used ajax for in the change view is to add a thumbnail into the view of stuff that have image fields.

I don't really have any good tutorials off the top of my head...though www.jquery.com has some great documentation on the stuff available there...Also I've never done Jquery to interact with the DB..

you might want to write a view and have jquery ping that view, to do the queries...and return a json object...that you can use to populate various fields in your admin form...that way you're still using django to query the db and do all the lifting.

hope that helps...

n


On Wed, Sep 15, 2010 at 5:24 PM, pixelcowboy <pixelcowboy79@gmail.com> wrote:
Mmmm seems like inheritance is not as straight forward as I thought it
was... Might be that this is the case that the admin doesnt cut it for
what I need it to do... Ill probably need to see ways in which I can
extend it. Like for example, I can think of adding a callback to the
FK selection button, that when clicked, queries the database and
prepopulates the inherited values. Any suggestions on how to implement
this kind of thing. I have never use jquery for example to interact
with a database, any good tutorials or tips on how to do this with
django? Thanks!


On Sep 15, 7:59 am, "nick.l...@gmail.com" <nick.l...@gmail.com> wrote:
> Good question! :)
>
> I don't know...like I said I hadn't thought it through! :)
> I'll ponder it a bit...
>
> maybe you'd have to tackle it in a different direction...Say something like
> this:
>
> you have a dropdown in your project model that's blankable/nullable and it
> would essentially be a FK to other projects...
>
> When you clicked save (or save and continue working) you would have a custom
> save method that would go through and pull out all the information you
> wanted about that other project and pull it in to the new project.
>
> Does that make sense? Unfortunatly, it means you have to click a button to
> load the inherited values...BUT this way you can make sure that the users
> are selecting the right project to inherit from...
>
> ALSO it would have to only work on the creation of an object...not the
> changing of an object. (ie so you don't override the new values with the
> inherited ones every time...)
>
> OR you could have it check to see if those values are null, if they are null
> put in the inherited values, else pass..
>
> n
>
> PS I'm just thinking of things off the top of my head! :)
> On Wed, Sep 15, 2010 at 4:16 AM, pixelcowboy <pixelcowbo...@gmail.com>wrote:
>
>
>
>
>
> > Im not sure Im getting this properly. In the example above, how would
> > you know what the word "whatever", the query value, stands for? How
> > could you keep it dynamic in that the values of that field is taken
> > from whatever the actual parent object is? Thanks.
>
> > On Sep 14, 2:34 pm, pixelcowboy <pixelcowbo...@gmail.com> wrote:
> > > I'll try when I get home, but thanks again for your help!
>
> > > On Sep 14, 11:31 am, "nick.l...@gmail.com" <nick.l...@gmail.com>
> > > wrote:
>
> > > > Hi again! :)
>
> > > > I haven't thought this idea through, and very well could be
> > > > wildly inefficient! :)
>
> > > > Though in your admin.py when registering the admin model do something
> > like
> > > > this:
>
> > > > from myproject.myapp.models import MyModel, OtherModel
>
> > > > class MyModelAdmin(models.admin):
> > > >     object_i_want = OtherModel.objects.get(field_i_care_about=whatever)
> > > >     prepopulated_fields = { "field_i_care_about":
> > > > (object_i_want.field_i_care_about)
>
> > > > admin.site.register(MyModel, MyModelAdmin)
>
> > > > On Tue, Sep 14, 2010 at 3:59 PM, pixelcowboy <pixelcowbo...@gmail.com
> > >wrote:
>
> > > > > Hi, I want a project structure that is as follows:
>
> > > > > I have an application with project model that has a subproject, and
> > > > > that subproject has other subproject,etc. What I want to get is that
> > > > > the subproject gets the same attributes that the parent project, but
> > > > > is also able to override their values in its own instance (but not in
> > > > > the parent instance).
>
> > > > > My first idea is using something like a foreign key. This gives me
> > > > > access to the parent values, but not the ability to override them.
>
> > > > > The second option is multi-table inheritance from the parent class.
> > > > > This gives me the parents class attributes, but I again I dont get
> > the
> > > > > fields in the admin, so Im not able to override them (at least not in
> > > > > the admin). Is there any way to this?
>
> > > > > The third option is to inherit an abstract class. The parent also
> > > > > inherits from this class, so both have the fields I need. However, I
> > > > > would like for the child class to inherit default values for this
> > > > > fields from the parent class, but be free to override them. Is there
> > > > > any way to pre-populate fields in the admin with the attribute values
> > > > > from a parent or related files?
>
> > > > > Thanks for your help.
>
> > > > > --
> > > > > 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+unsubscribe@googlegroups.com<django-users%2Bunsubscribe@google groups.com>
> > <django-users%2Bunsubscribe@google groups.com>
> > > > > .
> > > > > For more options, visit this group at
> > > > >http://groups.google.com/group/django-users?hl=en.
>
> > > > --
> > > > Guadajuko! Vamos a correr!
> > > >  -"Cool! we are going to run!"
>
> > --
> > 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+unsubscribe@googlegroups.com<django-users%2Bunsubscribe@google groups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/django-users?hl=en.
>
> --
> Guadajuko! Vamos a correr!
>  -"Cool! we are going to run!"

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--
Guadajuko! Vamos a correr!
 -"Cool! we are going to run!"

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment