On Jan 19, 5:25 am, 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

Hi,

I think here what you're looking for is to use:
address = models.ForeignKey(Address)
rather than a GenericRelation.  GenericRelations are for cases when
you don't know what the target model should be (hence the extra
content_type field).

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