Re: Proper approach to updating model object with 100 attributes.

2010-06-30 Thread Ray Cote
Thanks for everybody's comments. 
I ended up using the setattr method and, since I always know the data I have is 
a subset of the data in the model, that works wonderfully. 

--Ray


- Original Message -
From: "euan.godd...@googlemail.com" <euan.godd...@gmail.com>
To: "Django users" <django-users@googlegroups.com>
Sent: Wednesday, June 30, 2010 10:01:11 AM GMT -05:00 US/Canada Eastern
Subject: Re: Proper approach to updating model object with 100 attributes.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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: Proper approach to updating model object with 100 attributes.

2010-06-30 Thread euan.godd...@googlemail.com
The frozenset wasn't any commentary on your approach or speed of
parsing, I just like to use them where-ever possible.

My only concern with your approach was that you were expecting every
field on the model to be in the dictionary. Looping over the
dictionary's items and ignoring any fields that aren't on the model
seems most sensible to me since try/except is relatively slow in
python.

Oh and iteritems as opposed to items wasn't a slight on your coding
either, it's just that it's slightly more memory efficient (but since
items is becoming iteritems in python 3 it doesn't matter too much) :)

On Jun 30, 2:56 pm, Tim Chase  wrote:
> >>> I'd stick to setattr and maybe verify that the key in the dictionary
> >>> is one of the model's fields. I think there is a method on _meta
> >>> called get_all_field_names. I've used this before to validate such
> >>> actions.
>
> >> If that's the case, you can tweak the above to something like
>
> >>     for name in obj._meta.get_all_field_names():
> >>       if name in dict_of_field_values:
> >>         setattr(obj, name, dict_of_field_values[name])
>
> > I'd probably be a bit more cautious, since get_all_field_names gets
> > foreign keys and all sorts and the field might not be in the
> > dictionary.
>
> > I'd suggest:
>
> > # Make a frozenset of the fields for fast access:
> > allowed_fields = frozenset(obj._meta.get_all_field_names())
>
> > for field, value in dictionary_of_field_values.iteritems():
> >      if field in allowed_fields:
> >          setattr(obj, field, value)
>
> You're using different logic than I am, so yes, using a set (or
> frozen-set) would be better for your loop over the entries in the
> dict (my original code iterates over the defined field names and
> tries to look them up in the dict -- dict & set lookups are both
> O(1) so it only makes a difference if you're doing your lookups
> in the meta)
>
> -tkc

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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: Proper approach to updating model object with 100 attributes.

2010-06-30 Thread Tim Chase

I'd stick to setattr and maybe verify that the key in the dictionary
is one of the model's fields. I think there is a method on _meta
called get_all_field_names. I've used this before to validate such
actions.


If that's the case, you can tweak the above to something like

for name in obj._meta.get_all_field_names():
  if name in dict_of_field_values:
setattr(obj, name, dict_of_field_values[name])


I'd probably be a bit more cautious, since get_all_field_names gets
foreign keys and all sorts and the field might not be in the
dictionary.

I'd suggest:

# Make a frozenset of the fields for fast access:
allowed_fields = frozenset(obj._meta.get_all_field_names())

for field, value in dictionary_of_field_values.iteritems():
 if field in allowed_fields:
 setattr(obj, field, value)


You're using different logic than I am, so yes, using a set (or 
frozen-set) would be better for your loop over the entries in the 
dict (my original code iterates over the defined field names and 
tries to look them up in the dict -- dict & set lookups are both 
O(1) so it only makes a difference if you're doing your lookups 
in the meta)


-tkc



--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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: Proper approach to updating model object with 100 attributes.

2010-06-30 Thread euan.godd...@googlemail.com
I'd probably be a bit more cautious, since get_all_field_names gets
foreign keys and all sorts and the field might not be in the
dictionary.

I'd suggest:

# Make a frozenset of the fields for fast access:
allowed_fields = frozenset(obj._meta.get_all_field_names())

for field, value in dictionary_of_field_values.iteritems():
if field in allowed_fields:
setattr(obj, field, value)

Euan

On Jun 30, 1:36 pm, Tim Chase  wrote:
> On 06/30/2010 02:10 AM, euan.godd...@googlemail.com wrote:
>
> > I think you need to be careful messing with __dict__ as Django turns
> > most fields in descriptors behind the scenes so setting them in the
> > __dict__ could break these.
>
> Yeah, that was somewhat my assumption (and thus my caveat).
>
> >> Well, you could do something like
>
> >>     for name, value in dictionary_of_field_values.items():
> >>       setattr(obj, name, value)
>
> > I'd stick to setattr and maybe verify that the key in the dictionary
> > is one of the model's fields. I think there is a method on _meta
> > called get_all_field_names. I've used this before to validate such
> > actions.
>
> If that's the case, you can tweak the above to something like
>
>    for name in obj._meta.get_all_field_names():
>      if name in dict_of_field_values:
>        setattr(obj, name, dict_of_field_values[name])
>
> -tkc

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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: Proper approach to updating model object with 100 attributes.

2010-06-30 Thread Tim Chase

On 06/30/2010 02:10 AM, euan.godd...@googlemail.com wrote:

I think you need to be careful messing with __dict__ as Django turns
most fields in descriptors behind the scenes so setting them in the
__dict__ could break these.


Yeah, that was somewhat my assumption (and thus my caveat).


Well, you could do something like

for name, value in dictionary_of_field_values.items():
  setattr(obj, name, value)


I'd stick to setattr and maybe verify that the key in the dictionary
is one of the model's fields. I think there is a method on _meta
called get_all_field_names. I've used this before to validate such
actions.


If that's the case, you can tweak the above to something like

  for name in obj._meta.get_all_field_names():
if name in dict_of_field_values:
  setattr(obj, name, dict_of_field_values[name])

-tkc





--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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: Proper approach to updating model object with 100 attributes.

2010-06-30 Thread euan.godd...@googlemail.com
I think you need to be careful messing with __dict__ as Django turns
most fields in descriptors behind the scenes so setting them in the
__dict__ could break these.

I'd stick to setattr and maybe verify that the key in the dictionary
is one of the model's fields. I think there is a method on _meta
called get_all_field_names. I've used this before to validate such
actions.

Euan

On 29 June, 19:03, Tim Chase  wrote:
> On 06/29/2010 12:01 PM, Ray Cote wrote:
>
>
>
>
>
> > Hi List:
>
> > I have a Django model with over 100 fields in it that is loaded from a data 
> > feed.
> > Each row in the model has a unique field, let's call it item_id.
> > When loading new data, I'm first checking to see if item_id is in the table,
> > if it is, I want to update it with the new data from the new 100 fields.
>
> > To date, I've done things like:
>
> > obj = Model.objects.get(item_id = item_id_from_field)
>
> > and then.
> > obj.field1 = new_field1
> > etc.
>
> > However, for 100 fields, I'd like to find something a bit cleaner than 
> > listing 100 fieldnames.
> > The data for the new 100 fields is in a nice dictionary.
>
> > When I create a new item, I'm able to do this:
> >    obj = MyModel(**dictionary_of_field_values)
>
> > Is there something similar I can do with my obj once the data is retrieved?
>
> Well, you could do something like
>
>    for name, value in dictionary_of_field_values.items():
>      setattr(obj, name, value)
>
> or possibly even just
>
>    obj.__dict__.update(dictionary_of_field_values)
>
> (I'm not sure how this interacts with Django's meta-class
> yumminess, but it works for regular Python classes)
>
> -tkc

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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: Proper approach to updating model object with 100 attributes.

2010-06-29 Thread Lora

On 6/29/10 4:20 PM, Ray Cote wrote:

Hi Tim:

Thanks for the pointers.
I think the setattr is probably safest way to deal with the Django models.

--Ray

- Original Message -
From: "Tim Chase"<django.us...@tim.thechases.com>
To: django-users@googlegroups.com
Cc: "Ray Cote"<rgac...@appropriatesolutions.com>
Sent: Tuesday, June 29, 2010 2:03:05 PM GMT -05:00 US/Canada Eastern
Subject: Re: Proper approach to updating model object with 100 attributes.

On 06/29/2010 12:01 PM, Ray Cote wrote:
   

Hi List:

I have a Django model with over 100 fields in it that is loaded from a data 
feed.
Each row in the model has a unique field, let's call it item_id.
When loading new data, I'm first checking to see if item_id is in the table,
if it is, I want to update it with the new data from the new 100 fields.

To date, I've done things like:

obj = Model.objects.get(item_id = item_id_from_field)

and then.
obj.field1 = new_field1
etc.

However, for 100 fields, I'd like to find something a bit cleaner than listing 
100 fieldnames.
The data for the new 100 fields is in a nice dictionary.

When I create a new item, I'm able to do this:
obj = MyModel(**dictionary_of_field_values)

Is there something similar I can do with my obj once the data is retrieved?
 

Well, you could do something like

for name, value in dictionary_of_field_values.items():
  setattr(obj, name, value)

or possibly even just

obj.__dict__.update(dictionary_of_field_values)

(I'm not sure how this interacts with Django's meta-class
yumminess, but it works for regular Python classes)

-tkc




   
Perhaps you could use  django.forms.models.model_to_dict() method?  that 
is if you have existing object already and you need to retrieve  it's 
attributes into a nice dict.
PS. If I misunderstood your question, disregard everything i wrote and 
do accept my apologies :)



--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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: Proper approach to updating model object with 100 attributes.

2010-06-29 Thread Ray Cote
Hi Tim: 

Thanks for the pointers. 
I think the setattr is probably safest way to deal with the Django models. 

--Ray

- Original Message -
From: "Tim Chase" <django.us...@tim.thechases.com>
To: django-users@googlegroups.com
Cc: "Ray Cote" <rgac...@appropriatesolutions.com>
Sent: Tuesday, June 29, 2010 2:03:05 PM GMT -05:00 US/Canada Eastern
Subject: Re: Proper approach to updating model object with 100 attributes.

On 06/29/2010 12:01 PM, Ray Cote wrote:
> Hi List:
>
> I have a Django model with over 100 fields in it that is loaded from a data 
> feed.
> Each row in the model has a unique field, let's call it item_id.
> When loading new data, I'm first checking to see if item_id is in the table,
> if it is, I want to update it with the new data from the new 100 fields.
>
> To date, I've done things like:
>
> obj = Model.objects.get(item_id = item_id_from_field)
>
> and then.
> obj.field1 = new_field1
> etc.
>
> However, for 100 fields, I'd like to find something a bit cleaner than 
> listing 100 fieldnames.
> The data for the new 100 fields is in a nice dictionary.
>
> When I create a new item, I'm able to do this:
>obj = MyModel(**dictionary_of_field_values)
>
> Is there something similar I can do with my obj once the data is retrieved?

Well, you could do something like

   for name, value in dictionary_of_field_values.items():
 setattr(obj, name, value)

or possibly even just

   obj.__dict__.update(dictionary_of_field_values)

(I'm not sure how this interacts with Django's meta-class 
yumminess, but it works for regular Python classes)

-tkc




-- 
Ray Cote, President
Appropriate Solutions, Inc.
We Build Software
603.924.6079

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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: Proper approach to updating model object with 100 attributes.

2010-06-29 Thread Tim Chase

On 06/29/2010 12:01 PM, Ray Cote wrote:

Hi List:

I have a Django model with over 100 fields in it that is loaded from a data 
feed.
Each row in the model has a unique field, let's call it item_id.
When loading new data, I'm first checking to see if item_id is in the table,
if it is, I want to update it with the new data from the new 100 fields.

To date, I've done things like:

obj = Model.objects.get(item_id = item_id_from_field)

and then.
obj.field1 = new_field1
etc.

However, for 100 fields, I'd like to find something a bit cleaner than listing 
100 fieldnames.
The data for the new 100 fields is in a nice dictionary.

When I create a new item, I'm able to do this:
   obj = MyModel(**dictionary_of_field_values)

Is there something similar I can do with my obj once the data is retrieved?


Well, you could do something like

  for name, value in dictionary_of_field_values.items():
setattr(obj, name, value)

or possibly even just

  obj.__dict__.update(dictionary_of_field_values)

(I'm not sure how this interacts with Django's meta-class 
yumminess, but it works for regular Python classes)


-tkc



--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@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.