Hi Mike,

thanks a lot but that was unfortunately not what I ment. Your
assignmements are static in

In [2]: n = TestFun(name="mike", description="Testing is always
fun.")

but I need them to be dynamic. So in the above case NAME wouldn't be
hardcoded but come from a dictionary. {'name': 'mike'} like this.
I just found at least some way:

newCustomer = Customer()
    for key, value in params.iteritems():
        newCustomer.__setattr__(key, value)

where PARAMS is my dictionary. It works but I don't think this is
really elegant...


On 29 Apr., 13:00, Mike Ramirez <gufym...@gmail.com> wrote:
> On Wednesday 29 April 2009 03:24:48 am Dennis Schmidt wrote:
>
>
>
>
>
> > Hi there,
>
> > this might be a trivial question but my search within the django
> > documentation and google didn't give me any helpful results.
> > In Ruby on Rails you can simply do this
>
> > x = Model.new({:param_x => 'x', :param_y => 'y'})
>
> > where the params are some model fields. The hash you can provide there
> > can be created on the fly. How can I do this in django??? I don't know
> > which fields I will set at a certain point and for all the fields I
> > don't set there I want the model-field's default value to be used. But
> > since I can only hardcode which attributes / fields I will assign this
> > is not really possible.
>
> > But I guess there MUST be a way to this. Only how?
>
> > thanks in advance, Dennis
>
> Is this what you want:
>
> Sample Model:
>
> class TestFun(models.Model):
>         name = models.CharField(max_length=50)
>         url = models.URLField(blank=True, null=True)
>         description = models.TextField()
>         new = models.BooleanField(default=True)
>
>         def __unicode__(self):
>                 return "%s" %(self.name)
>
> Shell creating a new object with only the name and description parameters:
>
> In [1]: from testfun.models import TestFun
>
> In [2]: n = TestFun(name="mike", description="Testing is always fun.")
>
> In [3]: n.save()
>
> In [4]: objs = TestFun.objects.get(name__exact="mike")
>
> In [5]: objs.new
>
> Out[5]: True
>
> In [6]: objs.url
>
> In [7]: objs.description
>
> Out[7]: u'Testing is always fun.'
>
> In [8]: objs.name
>
> Out[8]: u'mike'
>
> As you can see, I instiated my model "TestFun" with only the name and
> description parameters, then saved it.  The new field was set with the
> default setting of true, url I didn't have to enter, since it was set
> null=True (blank is for forms and allowing this field to be blank during
> validation).
>
> I could have easily done TestFun(name="mike", description="Testing is always
> fun.", new=False) to use a non default value for the 'new' parameter.
>
> To sum it up models are treated like normal python classes.[1]
>
> there is also two shortcuts, Model.objects.create() which returns a bound
> model instance, which is already saved for you [2]
>
> In addition to that there is a Model.objects.get_or_create() which returns a
> set of the bound model and a bool for saying if it was created or not.
> get_or_create follows create() in the docs, see [2].
>
> Mike
>
> [1]http://docs.djangoproject.com/en/dev/ref/models/instances/?from=olddo...
>
> [2]http://docs.djangoproject.com/en/dev/ref/models/querysets/#create-kwargs
>
> --
> It is said that the lonely eagle flies to the mountain peaks while the lowly
> ant crawls the ground, but cannot the soul of the ant soar as high as the
> eagle?
>
>  signature.asc
> < 1 KBAnzeigenHerunterladen
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to