Hello,

I don't think you need a GenericRelation unless you're using a 
GenericForeignKey.

Would it work to use plan model.ForeignKey instead of GenericRelation?

Collin

On Monday, November 17, 2014 12:50:09 PM UTC-5, Shaun Livingston wrote:
>
> Hi, 
> I have two different apps that contain classes of the same name, and using 
> a related name in a GenericRelation doesn't seem to be helping. Here are 
> the details:
>
> In both of the apps I have "Source" and "Variable." They're not the same 
> for either app, so I can't just create one. These apps are called "hs_core" 
> and "ref_ts." In both apps I have a class called 
> "<app_name(CamelCase)>Metadata" that has a GenericRelation to both of the 
> Source and Variable classes. So I added a related_name in order to 
> differentiate between the two in the database. It didn't seem to have any 
> effect at all. Here are some snippets of the code (I can add more if you 
> need):
>
> In ref_ts:
> class Source(AbstractMetaDataElement):
>     term = 'Source'
>     derived_from = models.CharField(max_length=300)
>     organization = models.CharField(max_length=200, null=True)
>     source_description = models.CharField(max_length=400, null=True)
>
>     def __unicode__(self):
>         return self.derived_from
>
>     @classmethod
>     def create(cls, **kwargs):
>         if 'derived_from' in kwargs:
>             # check the source doesn't already exists - source needs to be 
> unique per resource
>             if 'metadata_obj' in kwargs:
>                 metadata_obj = kwargs['metadata_obj']
>                 metadata_type = 
> ContentType.objects.get_for_model(metadata_obj)
>                 src = Source.objects.filter(derived_from= 
> kwargs['derived_from'], object_id=metadata_obj.id, 
> content_type=metadata_type).first()
>                 if src:
>                     raise ValidationError('Source:%s already exists for 
> this resource.' % kwargs['derived_from'])
>                 src = 
> Source.objects.create(derived_from=kwargs['derived_from'], 
> organization=kwargs.get('organization'),source_description=kwargs.get('source_description'),
>  
> content_object=metadata_obj)
>                 return src
>             else:
>                 raise ValidationError('Metadata instance for which source 
> element to be created is missing.')
>         else:
>             raise ValidationError("Source data is missing.")
>
>     @classmethod
>     def update(cls, element_id, **kwargs):
>         src = Source.objects.get(id=element_id)
>         if src:
>             if 'derived_from' in kwargs:
>                 src.derived_from = kwargs['derived_from']
>             if 'organization' in kwargs:
>                 src.organization = kwargs['organization']
>             if 'source_description' in kwargs:
>                 src.source_description = kwargs['source_description']
>             src.save()
>         else:
>             raise ObjectDoesNotExist("No source element was found for the 
> provided id:%s" % element_id)
>
>     @classmethod
>     def remove(cls, element_id):
>         src = Source.objects.get(id=element_id)
>         if src:
>             src.delete()
>         else:
>             raise ObjectDoesNotExist("No source element was found for 
> id:%d." % element_id)
>
> class RefTSMetadata(CoreMetaData):
> ...
>     variables = generic.GenericRelation(Ref_TS_Variable, 
> related_name='ref_ts_variables')
>     sources = generic.GenericRelation(Ref_TS_Source, 
> related_name='ref_ts_sources')
> ...
>
>     @classmethod
>     def get_supported_element_names(cls):
>         # get the names of all core metadata elements
>         elements = super(RefTSMetadata, cls).get_supported_element_names()
>         # add the name of any additional element to the list
>         elements.append('Method')
>         elements.append('QualityControlLevel')
>         elements.append('Variable')
>         elements.append('Site')
>         return elements
>
>     @property
>     def resource(self):
>         return self._refts_resource.all().first()
>
>     def get_xml(self):
> ...
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c0222ff8-dacc-4a10-afe2-23e3f9f1adda%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to