On 8/23/06, richard mendes <[EMAIL PROTECTED]> wrote:
>
> Barry,
>
> Actually what i was thinking is to get a list with the keywords and
> then go with a forloop threw all keywords and get the values from a
> dictionary where the key's are the same as the keywords only in field
> names.
>
> so what i thought was get the keywords and do the following
> keywords = [ all the keywords from model ]
>
> for keyword in keywords:
> apa = LupusApa( keyword = dictionary[str(keyword)]
> apa.save()
>
> i thought this was the best solution there are two problems with this
> is one I couldnt figure out how i got all the keywords from the model
> in a array and the second was if i save the object apa i gives a error
> because there are some fields that can't be zero.
>
> and i don't know how to create a object apa with all the keywords
> after eachother like ( apa = LupusApa (result_id =
> dictionary['result_id'], lab_id = dictionary['lab_id] ) without
> hardcoding the names in there.
>
> if someone knows how to deal with these two problems it would be a
> great help to me.
>
> thanks in advance,
>
> richard mendes
>
There is a create method in Model Manager Class, and there is a sample
code you can see:
def save(self, data, object_id=None):
params = {}
for key in data:
for f in self.model._meta.fields:
if key == f.column:
params[key] = data[key]
if not object_id:
return self.model.objects.create(**params)
else:
obj = self.model.objects.get(pk=int(object_id))
for key, value in params.items():
if hasattr(obj, key):
setattr(obj, key, params[key])
obj.save()
return obj
This function can create a new object or update a exists object. I
hope it can be some useful.
--
I like python!
My Blog: http://www.donews.net/limodou
My Django Site: http://www.djangocn.org
NewEdit Maillist: http://groups.google.com/group/NewEdit
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---