Multiple different foreign objects as "resources"

Viewed 20

Update

This is what I've ended up doing:

class Agenda(models.Model):
    attendants = models.ManyToManyField("base.User", related_name="agendas", blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    date = models.DateTimeField(null=True)
    text = models.TextField()

    def get_restaurants(self):
        return self.resources.instance_of(RestaurantResource)

    def get_parking_lots(self):
        return self.resources.instance_of(ParkingLotResource)


class Resource(PolymorphicModel):
    agenda = models.ForeignKey(
        Agenda, related_name="resources", on_delete=models.CASCADE
    )
    comment = models.TextField(null=True)


class RestaurantResource(Resource):
    resource = models.ForeignKey(Restaurant, on_delete=models.CASCADE)


class ParkingLotResource(Resource):
    resource = models.ForeignKey(ParkingLot, on_delete=models.CASCADE)

Then for each business type connected to an agenda, I create its particular resource (like RestaurantResource). So, then I can go agenda_object.resources.all().

I'm still not happy thou. It feels superfluous. As now I have to go

RestaurantResource.objects.create(resource=inv, agenda=agenda, comment="whatever")
ParkingLotResource.objects.create(resource=pree, agenda=agenda, comment="somethi")

So I have to keep track of the type of resource connected instead of just being able to iterate over the resources and creating the objects.


Problem

So say I have a model, Agenda:

class Agenda(models.Model):
        ...
        resource_comments = models.Field...

class ResourceComment(models.Model):
        comment = models.TextField()
        resource = models.ForeignKey... | GenericRelation

Say I wanted resource_comment to be one or more of several other models, like.. Restaurant, PetrolStation, CarPark with a comment.

So in a meeting agenda we might want to talk about a particular restaurant and a couple of petrol stations nearby. Whatever, it's a usecase.

How would I go about this? Should I have a separate field for each..? That doesn't feel quite right... like I don't want

class Agenda(models.Model):
        ...
        restaurants = models....
        parking_lots = models..
        petrol_stations = models..
        ...

I suppose I could have a model like...

class Resource(models.Model):
         resource_id = Int
         resource_model = String # (e.g. Restaurant) 
         comment = String # (a comment pertaining to resource from having the agenda meeting) 

Kind of also feels off as I then have to manually get the objects.. Like

get restaurants_in_agenda(self):
       ids = self.filter(resource_model="Restaurant").value_list('ids')
       return Restaurant.objects.filter(id__in=ids)

Or, is this OK?

Another solution would be to make the Agenda the generic model, but I kind of don't want this field on all the business models... because.. what do I do then with the resource comment?

class Agenda(models.Model):
    attendants = models.ManyToManyField("base.User", related_name="agendas", blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    date = models.DateTimeField(null=True)
    text = models.TextField()

    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey("content_type", "object_id")

class OnAllBusinesses(models.Model):
      ...
      agendas = GenericRelation(Agenda)

Something else I've thought about is "through" so,

class Agenda(models.Model):
      restaurants = models.ManyToManyField(
                           "restaurant",
                           through="AgendaResource",
                           through_fields=("agenda", "restaurant"))
      ...

class AgendaResource(models.Model):
      restaurant = models.ForeignKey(...)
      agenda = models.ForeignKey(...)
      comment = models.TextField()
      ...

But then I have one for each business type again...

Thanks for any help.

0 Answers
Related