Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django : How to use models from another app

Tags:

django

I have got two apps: homepage and blog. I have a model Post in the app blog. I can use this model for the app blog but not for the app homepage.

How can I use this model in app homepage: I want to display some of my recent blog posts as links in my homepage.

blog/models.py

from django.db import models
from django.db.models import permalink

class Post(models.Model):
    title=models.CharField(max_length=140)
    body=models.TextField()
    date=models.DateTimeField()

    def __str__(self):
        return self.title

blog/urls.py

from django.conf.urls import url, include
from django.views.generic import ListView, DetailView
from blog.models import Post     
from . import views
urlpatterns = [ 
    url(r'^$', ListView.as_view(
        queryset=Post.objects.all().order_by("-date")[:25],
        template_name="blog/blog.php")),
]

This is how I displayed my recent blog post in app blog Template

{% for post in object_list %}
     <div id="post_list">
     <h2 class="header1"><a href="/blog/{{post.id}}">  {{ post.title }}</a></h2>
     <h5 class="date_time">{{ post.date }}</h5>
     {{ post.PostImage|safe }}
    <br>        
    <div id="button"><a href="/blog/{{post.id}}"><text class="buttonDefault" > READ POST </text></a></div>
    </div>
{% endfor %}

I want to display it similarly in homepage template.

like image 435
Ishan Avatar asked Mar 30 '16 06:03

Ishan


People also ask

How import all models in Django?

To do this, create a directory called /«project»/«app_name»/models , inside it put __init__.py (to declare it as a module) and then create your files inside there. You then need to import your file contents into the module in __init__.py . You should read about Python modules to understand this.

How do I import a model in Django?

from django.apps import apps and then whereever in your code you need to import a model you can do the following. model = apps.get_model('app_name', 'ModelName')

How to use models between multiple apps in Django?

[Django] Best way to use models between multiple apps? I'm building a Django app and I was asked split apart models.py and put the resulting models in a 'models' folder. I did that and it works fine. I was then asked to move the models folder to the Project level so that the models can be used by other apps.

How do I copy data from one Django model to another?

The Long Way: Copy the Data to a New Django Model 1 Create the New Model. Start by creating a new product app. ... 2 Copy the Data to the New Model. ... 3 Update Foreign Keys to the New Model. ... 4 Delete the Old Model. ... 5 Bonus: Reverse the Migrations. ... 6 Handle Special Cases. ... 7 Summary: Pros and Cons of Copying the Data. ...

What is a model in Django?

A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table. Each model is a Python class that subclasses django.db.models.Model. Each attribute of the model represents a database field.


1 Answers

You can use models from other apps by importing them the same way you imported permalink from the Django models:

from django.db.models import permalink

It's difficult to tell you the exact import without knowing your project structure. It could be something like this:

from project.apps.blog.models import Post

To make your life easier you could use a decent IDE that resolves imports for you (like PyCharm for example).

like image 145
Risadinha Avatar answered Jan 04 '23 00:01

Risadinha