heya,

The thing is, it's a one-to-many for UserProfile/Hospital/Institution
to Addresses - that is, each entity can (and probably will) have
multiple addresses. That's why the FK field is on Address.

Also, I thought that was the way it was traditionally modelled in
database design? (I admit I'm often confused by this part, so anybody
that can shed light on the rationale for one way or another, please
chime in).

Cheers,
Victor

On Jan 20, 12:20 am, LostCruz <lost.crusa...@gmail.com> wrote:
> Hi Victor,
>
> You are trying to do this the wrong way around. It shouldn't be the
> Adress class on which you want to define a relationship, but on the
> other classes. An address is an address whether it's the address of a
> User or the address of an Institution.
>
> class Address(models.Model):
>     # the field up until and including 'country'
>
> class Hostpital(models.Model):
>     name = models.CharField(max_length=20)
>     address = ForeignKey(Address)
>
> class YourUserClass(models.Model):
>     # all kinds of fields
>     address = ForeignKey(Address)
>
> Using Django's ORM system you then can get related Users and Hospitals
>
> Hope this helps.
>
> On 19 jan, 06:25, Victor Hooi <victorh...@gmail.com> wrote:
>
>
>
> > heya,
>
> > I'm trying to use an "Address" model as a generic relation against
> > multiple other models (e.g. in a "User Profile", for each User, as
> > well as for "Building", "Institution", and various other ones).
>
> > So I've added the content_type, object_id and content_object fields to
> > Address.
>
> > class Address(models.Model):
> >     """
> >     Basic object for holding address information - for either people
> > or institutions.
> >     """
> >     street_address = models.CharField(max_length=50)
> >     suburb = models.CharField(max_length=20)
> >     state = models.CharField(max_length=3,
> > choices=AUSTRALIAN_STATES_CHOICES)
> >     # PositiveIntegerField doesn't allow max_length? Have to use form
> > validation for this.
> >     postcode = models.CharField(max_length=4)
> >     # This should probably be a list of choices.
> >     country = models.CharField(max_length=20)
> >     address_category = models.OneToOneField(AddressCategory)
>
> >     # We make Address a generic relation model
> >     content_type = models.ForeignKey(ContentType)
> >     object_id = models.PositiveIntegerField()
> >     content_object = generic.GenericForeignKey('content_type',
> > 'object_id')
>
> >     class Meta:
> >         verbose_name_plural = 'Addresses'
>
> >     def __unicode__(self):
> >         return self.street_address + ', ' + self.suburb
>
> >  And my other objects have GenericRelations to Address. E.g.:
>
> > class Hospital(models.Model):
> >     name = models.CharField(max_length=20)
> >     address = generic.GenericRelation(Address)
>
> >     def __unicode__(self):
> >         return self.name
>
> > However, when I try and use the Django admin to add an Address item,
> > the form has fields for content_type and object_id. I had thought
> > these fields wouldn't appear? Also, the model won't validate/save if I
> > don't fill in these fields. I don't think I'm quite using this right -
> > is there a better way of doing what I'm trying to achieve here?
>
> > Also, I'm using the Address object as in Inline for the other models,
> > so I have an Address Inline as well (Nb: I'm using django-reversion
> > with this site, hence the "VersionAdmin")
>
> > class AddressAdmin(VersionAdmin):
> >     pass
> > class AddressInline(generic.GenericTabularInline):
> >     model = Address
> > ...
> > class HospitalAdmin(admin.ModelAdmin):
> >     inlines = [
> >         AddressInline,
> >     ]
> > ...
> > admin.site.register(Address, AddressAdmin)
>
> > Thanks,
> > Victor
-- 
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.


Reply via email to