-1

I honestly don't know how to even phrase my question but i think an example might help.

I have mad three apps with their own models respectfully. App1, App2, App3. I want to create a new app (Reports) which will report on each of my other apps (App1,App2,App3). With my current knowledge i have the following in my Reports models.py

class Reports(models.Model):
    name = models.CharField(max_length=150, blank=True)

    app1_reported = models.ForeignKey(
        App1, on_delete=models.CASCADE, related_name='reported_app1', blank=True)

    app2_reported = models.ForeignKey(
        App2, on_delete=models.CASCADE, related_name='reported_app2', blank=True)

    app3_reported = models.ForeignKey(
        App3, on_delete=models.CASCADE, related_name='reported_app3', blank=True)

is there any way for me to create in my Reports model one reference that could reference either App1, or App2, or App3? something like:

class Reports(models.Model):
    name = models.CharField(max_length=150, blank=True)

    reported = models.ForeignKey(
        App1 or App2 or App3, on_delete=models.CASCADE, related_name='reported_app_is', blank=True)

My app works fine with the first implementation I would just like to know if there is a better way. Also please let me know if this makes sense, im struggling with the right wording.

  • If i understand, you have three apps, each one has a Model with the same name ? – Guillaume Jan 14 '21 at 18:15
  • @BriseBalloches yes sir thats correct. – FlawlessCalamity Jan 14 '21 at 18:17
  • 1
    This may help you https://stackoverflow.com/a/881874/4151233 – Marco Jan 14 '21 at 18:19
  • Thanks @Marco, I checked out the link and managed to get it working to a certain extent. I also check the docs that are suggested in the post as well as suggested by Abdul below. However I cant seem to get the relation reference in my model. When using "fields.GenericRelation(Faves)" as per the post but obviously relating to my app as well as going through the docs following through with what they suggest. It doesnt show up in the admin. any ideas? – FlawlessCalamity Jan 17 '21 at 12:35
  • @FlawlessCalamity what doesn't show in the admin, can you specify? – Abdul Aziz Barkat Jan 17 '21 at 12:48
  • @AbdulAzizBarkat, So in my above example in the admin interface if i view either App1 or App2 or App3, the fields do not show the relationship to Reports. – FlawlessCalamity Jan 17 '21 at 13:04
  • @FlawlessCalamity App1, App2 or App3 don't point to Report, Report points to them. – Abdul Aziz Barkat Jan 17 '21 at 13:22
  • @AbdulAzizBarkat but wouldnt the Foreign key relationship create a related name in the App model pointing back to the specific report? – FlawlessCalamity Jan 18 '21 at 06:35

1 Answers1

2

You can use the contenttypes framework. Here is a link for your reference

Abdul Aziz Barkat
  • 19,475
  • 3
  • 20
  • 33