开发者

AttributeError: 'GenericRelatedObjectManager' object has no attribute

开发者 https://www.devze.com 2022-12-07 18:40 出处:网络
I tried to use generic relation in django project. but it gives attribute error. Generic relation is between UniqueSourcePresenter and UniqueDbSource

I tried to use generic relation in django project. but it gives attribute error. Generic relation is between UniqueSourcePresenter and UniqueDbSource

i have an instance of TextTable which has a foreignkey attribute for UniqueSourcePresenter i tried to use reverse relation as self.presenter.texttable in UniqueDbSource's instance method. but it gives error es below

File "/usr/src/app/apps/tableau/models/source.py", line 191, in create_datum
backend     |     if self.presenter.texttable is not None:
backend     | AttributeError: 'GenericRelatedObjectManager' object has no attribute 'texttable'

My models are like follows

class UniqueSourcePresenter(models.Model):
    """
    Generic Relation Manager of all types of source
    """
    # Below the mandatory fields for generic relation
    limit = models.Q(app_label = 'tableau', model = 'UniqueDbSource'.lower()) | models.Q(app_label = 'tableau', model = 'UniqueAPISource'.lower())
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, limit_choices_to = limit)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey()
    pass
class DataSource(models.Model):
    pass
class UniqueSource(DataSource):
    presenter = GenericRelation(UniqueSourcePresenter)

    class Meta:
        abstract = True
class UniqueDbSource(UniqueSource):
    """
    THIS CLASS ADDED TO CONTENTTYPE
    """
    def create_datum(self):
        """ THIS METHOD CALLED WITH SIGNAL """
        d = DatumDb()
        d.title = f"{self.title}_datum"
        if self.presenter.texttable is not None: ## THIS LINE GIVES ERROR
            d.connector = self.presenter.texttable.init_source.data.connector
            query = self.presenter.texttable.init_source.data.query
        elif self.presenter.charttable is not None:
            d.connector = self.charttable.presenter.init_source.data.connector
            query = self.charttable.presenter.init_source.data.query
        query.pk = None
        query.save()
        d.query = query
        d.save()
        self.data = d
    pass

    def create_presenter(self):
        p = UniqueSourcePresenter()
        p.content_type = ContentType.objects.get(app_label='tableau', model='UniqueDbSource'.lower())
        p.object_id = self.id
        p.save()
class Table(models.Model):
    unique_source = models.OneToOneField(UniqueSourcePresenter, on_delete=models.CASCADE, null=True, blank=True, related_name="%(class)s")

    class Meta:
        abstract = True
class TextTable(Table):
    pass   

    def create_unique_source(self):
        """
        create a unique source object if created
        and add selfs 
        """
        if self.init_source.content_type.model == 'InitialDbSource'.lower():
            us = UniqueDbSource()
            us.title = f"{self.title}_unique_source"
            us.save()
            self.unique_source = us.presenter
        elif self.init_source.content_type.model == 'InitialAPISource'.lower():
            us = UniqueAPISource()
            us.title = f"{self.title}_unique_source"
            us.save()
            self.unique_source = us.presenter
        pass

I added pass words where is not about my problem.

EDIT: signal.py

@receiver(post_save, sender=UniqueDbSource)
@receiver(post_save, sender=UniqueAPISource)
def after_unique_source_save(sender, instance, created, **kwargs):
    if created:
        instance.create_datum()
        instance.create_presenter()
        instance.__class__.objects.filter(id=instance.id).update(
            data=DatumDb.objects.all().last())
    else:
        pass

@receiver(post_save, sender=TextTable)
def after_table_save(sender, instance, created, **kwargs):
    if created:
        pass
        instance.create_unique_source()
    pas开发者_如何学JAVAs
    if created:
        TextTable.objects.filter(id=instance.id).update(
            unique_source=UniqueSourcePresenter.objects.all().last()
        ) # may you can also give a good advice for this line
0

精彩评论

暂无评论...
验证码 换一张
取 消