Re: Generic Relations and Django Admin?
heya, Hmm, I'm using generic.GenericTabularInline for the Address Inline, is that the same? class AddressAdmin(VersionAdmin): exclude = ('content_type', 'object_id',) ... class AddressInline(generic.GenericTabularInline): model = Address ... class HospitalAdmin(admin.ModelAdmin): inlines = [ AddressInline, ] ... Is there an error in the above? Also, I'm still confused on whether I should have that exclude in there? And the bug that Raffaele Salmaso pointed out (http:// code.djangoproject.com/ticket/12577), is that what's causing this? (The traceback in the ticket is quite different to the traceback I'm getting). Cheers, Victor Cheers, Victor On Jan 19, 9:55 pm, Daniel Rosemanwrote: > On Jan 19, 10:35 am, Victor Hooi wrote: > > > heya, > > > Thanks for the reply =). > > > I tried that, and the fields aren't there, but when I try to save the > > object, I get a: > > > IntegrityError at /admin/people/address/add/ > > people_address.content_type_id may not be NULL > > > so obvoiusly Django doesn't like it if those fields aren't filled. > > > How do people normally do this sort of thing, with a polymorphic > > object that's referenced by multiple other objects? > > Edit the address inline with the parent object (hospital, etc) by > using the generic.GenericInlineAdmin class - works exactly the same as > a normal inline admin class, but using generic relations. > -- > DR. -- 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: Generic Relations and Django Admin?
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, LostCruzwrote: > 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 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.
Re: Generic Relations and Django Admin?
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 Hooiwrote: > 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.
Re: Generic Relations and Django Admin?
On Jan 19, 10:35 am, Victor Hooiwrote: > heya, > > Thanks for the reply =). > > I tried that, and the fields aren't there, but when I try to save the > object, I get a: > > IntegrityError at /admin/people/address/add/ > people_address.content_type_id may not be NULL > > so obvoiusly Django doesn't like it if those fields aren't filled. > > How do people normally do this sort of thing, with a polymorphic > object that's referenced by multiple other objects? > Edit the address inline with the parent object (hospital, etc) by using the generic.GenericInlineAdmin class - works exactly the same as a normal inline admin class, but using generic relations. -- DR. -- 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: Generic Relations and Django Admin?
Victor Hooi wrote: > I tried that, and the fields aren't there, but when I try to save the > object, I get a: > > IntegrityError at /admin/people/address/add/ > people_address.content_type_id may not be NULL > > so obvoiusly Django doesn't like it if those fields aren't filled. it's a bug http://code.djangoproject.com/ticket/12577 > How do people normally do this sort of thing, with a polymorphic > object that's referenced by multiple other objects? use this hack until it's fixed if 'django.contrib.contenttypes' in settings.INSTALLED_APPS: # patch django.contrib.contenttypes.generic.BaseGenericInlineFormSet # to provide the instance.pk # see http://code.djangoproject.com/ticket/12577 from django.contrib.contenttypes import generic def save_new(self, form, commit=True): # Avoid a circular import. from django.contrib.contenttypes.models import ContentType kwargs = { self.ct_field.get_attname(): ContentType.objects.get_for_model(self.instance).pk, self.ct_fk_field.get_attname(): self.instance.pk, } new_obj = self.model(**kwargs) return generic.save_instance(form, new_obj, commit=commit) setattr(generic.BaseGenericInlineFormSet, 'save_new', save_new) -- ()_() | That said, I didn't actually _test_ my patch. | + (o.o) | That's what users are for! | +---+ 'm m' | (Linus Torvalds) | O | (___) | raffaele dot salmaso at gmail dot com | -- 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: Generic Relations and Django Admin?
heya, Thanks for the reply =). I tried that, and the fields aren't there, but when I try to save the object, I get a: IntegrityError at /admin/people/address/add/ people_address.content_type_id may not be NULL so obvoiusly Django doesn't like it if those fields aren't filled. How do people normally do this sort of thing, with a polymorphic object that's referenced by multiple other objects? Cheers, Victor On Jan 19, 5:59 pm, Raffaele Salmasowrote: > Victor Hooi wrote: > > class AddressAdmin(VersionAdmin): > > pass > > class AddressInline(generic.GenericTabularInline): > > model = Address > > ... > > fields = (the fields you want to display) > or > exclude = ('content_type', 'object_id',) > > > class HospitalAdmin(admin.ModelAdmin): > > inlines = [ > > AddressInline, > > ] > > ... > > admin.site.register(Address, AddressAdmin) > > -- > ()_() | That said, I didn't actually _test_ my patch. | + > (o.o) | That's what users are for! | +---+ > 'm m' | (Linus Torvalds) | O | > (___) | raffaele dot salmaso at gmail dot com | -- 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: Generic Relations and Django Admin?
heya, The thing is, the foreign key field is on the Address object, linking to another object that *has* an address. AFAIK, that's how it's meant to be in database design. That's why I need to put something there - e.g. class Address(models.Model): ... user = models.ForeignKey(UserProfile) but that doesn't work for hospitals, or say institutions, or anything else that needs an address. So I can't just pick one. Or is my design off? Cheers, Victor On Jan 19, 6:03 pm, greatlemerwrote: > On Jan 19, 5:25 am, Victor Hooi 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.
Re: Generic Relations and Django Admin?
On Jan 19, 5:25 am, Victor Hooiwrote: > 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.
Re: Generic Relations and Django Admin?
Victor Hooi wrote: > class AddressAdmin(VersionAdmin): > pass > class AddressInline(generic.GenericTabularInline): > model = Address > ... fields = (the fields you want to display) or exclude = ('content_type', 'object_id',) > class HospitalAdmin(admin.ModelAdmin): > inlines = [ > AddressInline, > ] > ... > admin.site.register(Address, AddressAdmin) -- ()_() | That said, I didn't actually _test_ my patch. | + (o.o) | That's what users are for! | +---+ 'm m' | (Linus Torvalds) | O | (___) | raffaele dot salmaso at gmail dot com | -- 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.
Generic Relations and Django Admin?
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.