dizuane
dizuane

Reputation: 1

How can you dynamically embed one app into another in Django 2.1?

Consider a site built using DJango (2.1) with 2 separate apps - a forum app and a poll app. How can I dynamically include the rendering of a poll app within the forum app (say as part of a forum post).

For instance, as a user, I would write my post and click an "embed poll" button. The modelform for the poll app would pop up, I would enter my info and save the poll. All of this make sense.

The part I'm having trouble with is being able to store the information of that poll as part of the forum.. i.e. when I go to view that post, I should see the poll associated with it.

The problem with including a poll as part of the forum app is that a poll could exist in other places (say a blog entry or on a simple front page).

What process(es) would be used to accomplish this?

Upvotes: 0

Views: 156

Answers (1)

Dipesh Bajgain
Dipesh Bajgain

Reputation: 839

If you have provided with your some codes and what you are trying to achieve that would be great. But if I have understood your problem then I hope you are looking for a solution as:

your poll database may be like:

class PollQuestion(models.Model):
    question = models.TextField()

class PollAnswer(models.Model):
    question = models.ForeignKey(PollQuestion, on_delete=models.CASCADE)
    answer = models.CharField(max_length=200)

Now you want your poll models to appear on your forum app views.py then you could call your models in forum app views.py as:

from polls.models import PollQuestion, PollAnswer

def embed_poll(request):
    # Here your code logic to implement poll questions and answer

Hope this can help you. And please try to make your question more clear with some of your code work so that stackoverflow community could help you with the much more precise answer than this.

your Question may also be possible duplicate of How to import models from one app to another app in Django?

Upvotes: 1

Related Questions