Re: Saving Child Records via ForeignKeyField

2010-04-29 Thread Atamert Ölçgen
You initialize (__init__) your ZoneType model instead of create()'ing. When 
you save an initialized it doesn't get an id magically.

I suggest you to use ZoneType.objects.create() whenever possible. But if you 
must init your model first; make sure you get() it again to retrieve its id. 
Remember that id's are created on database and they're non-existent before 
save.

-- 
Saygılarımla,
Atamert Ölçgen

 -+-
 --+
 +++

www.muhuk.com
mu...@jabber.org



A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?

-- 
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: Saving Child Records via ForeignKeyField

2010-04-29 Thread bheathr
Here are the models:
class ZoneType(models.Model):
name = models.CharField(max_length=50)
color = models.CharField(max_length=7)
class Meta:
app_label = 'maps'


class Zone(models.Model):
name = models.CharField(max_length=50)
type = models.ForeignKey(ZoneType)
code = models.CharField(max_length=10)
description = models.CharField(max_length=255)
uri = models.CharField(max_length=200)
class Meta:
app_label = 'maps'

My problem with using the QuerySet.add example is that I am importing
this data from a file that may have data scattered throughout it.
Here is the order I have to do things: create the parent record,
create the child record, associate it to the parent, set attributes of
child and parent, then save.  Perhaps I have to associate it with a
separate data structure and then use QuerySet.add().  I am doing all
this using getattr(), ReverseSingleRelatedObjectDescriptor, etc, so it
ends up being a more complicated.  The code below works and is more
like the real code:

#Definition would be loaded from file
parent_model = ZoningType
child_model = Zoning
rel_field = 'type'
parent_attributes = {'name':'ABC', 'color':'#123123'}
child_attributes = [{'name':'ABC-1', 'code':'ABA', 'description':'my
description', 'uri':'http://abc.com/abc'}, {'name':'ABC-2',
'code':'ABA', 'description':'my description', 'uri':'http://abc.com/
abc'}]
#Definition would be loaded from file

save_packages = []

#Setup Parent
parent_object = parent_model()
save_package = [parent_object, {}]
for attr, val in parent_attributes.items(): setattr(parent_object,
attr, val)

#Setup Child
for child in child_attributes:
 child_object = child_model()
 rel_descriptor = getattr(child_model, rel_field)
 if not save_package[1].has_key(rel_descriptor) :
  save_package[1][rel_descriptor] = []
 save_package[1][rel_descriptor].append(child_object)
 for attr, value in child.items(): setattr(child_object, attr,
value)

save_packages.append(save_package)


#Save Packages
for r in save_packages:
 r[0].save()
 for rd in r[1]:
  query_set = getattr(parent_object,
rel_descriptor.field.related.get_accessor_name())
  for child_object in r[1][rd]:
   query_set.add(child_object)

Thanks for pointing me in the right direction with the query set.  It
still seems like the models could do this for me when it looks up the
values from ForeignKeyField objects.  If they don't have a key, save
them and update the key.  In addition, it would always be in the same
transaction as well.  I'll have to manually ad that logic as well.  If
there is no easier way, I'll have to go with this.

On Apr 29, 1:40 am, akonsu  wrote:
> hello,
>
> it would be easier to help if you provided your modes. are you missing
> this:http://docs.djangoproject.com/en/dev/ref/models/relations/#ref-models...
>
> for example:
>
> >>> b = Blog.objects.get(id=1)
> >>> e = b.entry_set.create(
>
> ... headline='Hello',
> ... body_text='Hi',
> ... pub_date=datetime.date(2005, 1, 1)
> ... )
>
> On Apr 29, 2:16 am, "Robinson B. Heath" 
> wrote:
>
>
>
>
>
> > I believe I have done my due diligence, but point me in the right direction 
> > if I am missing something.
>
> > I am working on a generic importing engine to import various file 
> > formats(csv, fixed length, etc) into django models based on json formatted 
> > file definitions.  It needs to do something like the following, which I 
> > have actually tried in the shell.
>
> > zone_type = ZoneType()
> > zone_type.name = 'GrowthArea'
> > …
> > zone = Zone()
> > zone.type = zone_type
> > zone.name = 'TX-Region12'
>
> > #-save starts here
> > zone.save()
> > #-save ends here
>
> > Is this supposed to work?
>
> > I have also tried:
>
> > #-save starts here
> > zone_type.save()
> > zone.save()
> > #-save ends here
>
> > Both report that the foreign key field is null such as: " null value in 
> > column "type_id" violates not-null constraint".
>
> > This does work:
>
> > #-save starts here
> > zone_type.save()
> > zone.type = zone_type
> > zone.save()
> > #-save ends here
>
> > as does this:
>
> > #-save starts here
> > z.type.save()
> > z.type = z.type
> > z.save()
> > #-save ends here
>
> > I could do something like the last example in my code, but I'd rather not.  
> > It seems like this would be simple to do inside the model save logic.  Am I 
> > missing something?
>
> > Thanks.
>
> > --
> > 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 
> > athttp://groups.google.com/group/django-users?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this 

Re: Saving Child Records via ForeignKeyField

2010-04-29 Thread akonsu
hello,

it would be easier to help if you provided your modes. are you missing
this: 
http://docs.djangoproject.com/en/dev/ref/models/relations/#ref-models-relations

for example:

>>> b = Blog.objects.get(id=1)
>>> e = b.entry_set.create(
... headline='Hello',
... body_text='Hi',
... pub_date=datetime.date(2005, 1, 1)
... )


On Apr 29, 2:16 am, "Robinson B. Heath" 
wrote:
> I believe I have done my due diligence, but point me in the right direction 
> if I am missing something.
>
> I am working on a generic importing engine to import various file 
> formats(csv, fixed length, etc) into django models based on json formatted 
> file definitions.  It needs to do something like the following, which I have 
> actually tried in the shell.
>
> zone_type = ZoneType()
> zone_type.name = 'GrowthArea'
> …
> zone = Zone()
> zone.type = zone_type
> zone.name = 'TX-Region12'
>
> #-save starts here
> zone.save()
> #-save ends here
>
> Is this supposed to work?
>
> I have also tried:
>
> #-save starts here
> zone_type.save()
> zone.save()
> #-save ends here
>
> Both report that the foreign key field is null such as: " null value in 
> column "type_id" violates not-null constraint".
>
> This does work:
>
> #-save starts here
> zone_type.save()
> zone.type = zone_type
> zone.save()
> #-save ends here
>
> as does this:
>
> #-save starts here
> z.type.save()
> z.type = z.type
> z.save()
> #-save ends here
>
> I could do something like the last example in my code, but I'd rather not.  
> It seems like this would be simple to do inside the model save logic.  Am I 
> missing something?
>
> Thanks.
>
> --
> 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 
> athttp://groups.google.com/group/django-users?hl=en.

-- 
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.



Saving Child Records via ForeignKeyField

2010-04-29 Thread Robinson B. Heath
I believe I have done my due diligence, but point me in the right direction if 
I am missing something.

I am working on a generic importing engine to import various file formats(csv, 
fixed length, etc) into django models based on json formatted file definitions. 
 It needs to do something like the following, which I have actually tried in 
the shell.

zone_type = ZoneType()
zone_type.name = 'GrowthArea'
…
zone = Zone()
zone.type = zone_type
zone.name = 'TX-Region12'

#-save starts here
zone.save()
#-save ends here

Is this supposed to work?

I have also tried:

#-save starts here
zone_type.save()
zone.save()
#-save ends here

Both report that the foreign key field is null such as: " null value in column 
"type_id" violates not-null constraint".

This does work:

#-save starts here
zone_type.save()
zone.type = zone_type
zone.save()
#-save ends here

as does this:

#-save starts here
z.type.save()
z.type = z.type
z.save()
#-save ends here

I could do something like the last example in my code, but I'd rather not.  It 
seems like this would be simple to do inside the model save logic.  Am I 
missing something?

Thanks.

-- 
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.