如何将一个应用程序的对象导入同一个 django 项目中的另一个应用程序?
Posted
技术标签:
【中文标题】如何将一个应用程序的对象导入同一个 django 项目中的另一个应用程序?【英文标题】:How to import an object of one app into another app in the same django project? 【发布时间】:2022-01-16 06:28:28 【问题描述】:代码如下。
users.model.py
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from django.db import models
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from django.db import models
# from django.contrib.auth.models import User
from PIL import Image
from .managers import CustomUserManager
from django.contrib import admin
from django.contrib.auth.models import AbstractUser
from django.utils.translation import ugettext_lazy as _
class CustomUser(AbstractUser):
username = None
first_name = models.CharField('Name',max_length=200,unique=True)
email = models.EmailField(_('email address'), unique=True)
registration = models.IntegerField()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = CustomUserManager()
def __str__(self):
return self.email
class Profile(models.Model):
user = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
image = models.ImageField(default='default.jpg', upload_to='profile_pics')
blog.model.py
from django.db import models
from django.utils import timezone
from django.urls import reverse
from django.conf import settings
from multiselectfield import MultiSelectField
from django import forms
from django.core.validators import MinValueValidator, MaxValueValidator
from PIL import Image
DAYS_OF_WEEK = [
(0, ' Monday'),
(1, ' Tuesday'),
(2, ' Wednesday'),
(3, ' Thursday'),
(4, ' Friday'),
(5, ' Saturday'),
(6, ' Sunday'),
]
class PostManager(models.Manager):
def like_toggle(self, user, post_obj):
if user in post_obj.liked.all():
is_liked = False
post_obj.liked.remove(user)
else:
is_liked = True
post_obj.liked.add(user)
return is_liked
class Post(models.Model):
author = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField('Doctor\'s Name',max_length=100)
content = models.CharField('Specialty',max_length=100)
chamber = models.CharField('Chamber\'s Name',max_length=200)
address = models.CharField('Address',max_length=100, blank=True)
fees = models.IntegerField(default=0)
days = MultiSelectField('Available Days', choices= DAYS_OF_WEEK)
start_time = models.TimeField('Chamber Beginning Time')
end_time = models.TimeField('Chamber Ending Time')
image = models.ImageField( upload_to='profile_pics')
review = models.TextField()
rating = models.IntegerField('Behavior')
overall_rating = models.PositiveIntegerField(validators=[
MaxValueValidator(10),
MinValueValidator(0)
])
liked = models.ManyToManyField(
settings.AUTH_USER_MODEL, blank=True, related_name='liked')
date_posted = models.DateTimeField(default=timezone.now)
objects = PostManager()
class Meta:
ordering = ('-date_posted', )
def get_absolute_url(self):
return reverse('post_detail', kwargs='pk': self.pk)
def save(self):
super().save() # saving image first
img = Image.open(self.image.path) # Open image using self
if (img.height > 1020 or img.width > 1920):
new_img = (1020, 1920)
img.thumbnail(new_img)
img.save(self.image.path) # saving image at the same path
class Comment(models.Model):
post = models.ForeignKey(
Post, related_name='comments', on_delete=models.CASCADE)
author = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
approved_comment = models.BooleanField(default=True)
def approve(self):
self.approved_comment = True
self.save()
def get_absolute_url(self):
return reverse("post_list")
def __str__(self):
return self.author
我创建了一个项目。在该项目中,有两个名为:用户和博客的应用程序。我将 CustomUsers 保存在配置文件中。 我想使用 user.model.py 文件的 first_name 字段值作为 blog.model.py 文件的标题字段。 我是 django 新手。帮我解决这个问题!
【问题讨论】:
【参考方案1】:在博客应用中做
from user.models import model1,model2
在用户应用程序中,您可以导入博客模型,如
from blog.models import model1,model2
【讨论】:
以上是关于如何将一个应用程序的对象导入同一个 django 项目中的另一个应用程序?的主要内容,如果未能解决你的问题,请参考以下文章
如何将 python 函数从另一个文件导入 django 视图
将 django cms 作为应用程序导入现有的 django 项目