Skip to content

question #8

@cfdbwrbq

Description

@cfdbwrbq

Hi. Thanks for your example! Its very cool and clear.
Its not an issue, but question. I want to do the same as you, but using "GenericRelation". I can't find any code examples for that. Exactly zero.
What to do if my model (not yours BookImage but very similar) with GenericForeignKey and task is exactly like yours in your example. Create generic nested formset for an simple formset and point it to a parent model.
is it feasible at all or not? I would be grateful for any advice. i am stuck.

class BookImage(models.Model):
    "e.g. image of the cover, backcover, etc."

    image = models.ImageField(null=False, blank=False, max_length=255)
    alt_text = models.CharField(null=False, blank=True, max_length=255)
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

and

class Book(models.Model):
    title = models.CharField(null=False, blank=False, max_length=255)
    publisher = models.ForeignKey(
        "Publisher",
        null=False,
        blank=False,
        on_delete=models.CASCADE,
        related_name="books",
    )
    images = GenericRelation('BookImage', related_query_name='book_images')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions