2017-12-12 4 views
-1

저는 장고 세계에서 새롭고 두 변수를 models.py에서 연결하여 각 변수를 서로 동일하게 설정할 수 있습니다. 여기에 models.py 코드는 다음과 같습니다한 클래스의 변수를 Django models.py의 다른 클래스에있는 변수로 설정하는 방법은 무엇입니까?

from django.db import models 
from django.core.urlresolvers import reverse 
# Create your models here. 


class file(models.Model): 
    title = models.CharField(max_length=250) 
    FILE_TYPE_CHOICES = (
     ('audio','Audio'), 
     ('games','Games'), 
     ('videos','Videos'), 
     ('applications','Applications'), 
     ('books','Books/Docs'), 
     ('others','Others') 
     ) 
    file_type = models.CharField(max_length=10,choices=FILE_TYPE_CHOICES,default='others') 
    description = models.TextField(max_length=6000) 
    #uploader_username = ??? 

    def get_absolute_url(self): 
     return reverse('one:user') 

    def __str__(self): 
     return self.title 

class user (models.Model): 
    username= models.CharField(max_length=100) 
    email=models.EmailField 
    password= models.CharField(max_length = 100) 
    user_files = models.ForeignKey(file, on_delete=models.CASCADE) 

내가 클래스 user 클래스에서 username에 동일 uploader_usernamefile에서 설정합니다.

+0

'uploader_username = user.username'? –

+0

기본적으로 사용자와 파일 모델간에 연관성을 갖고 싶을 것입니다. https://docs.djangoproject.com/en/2.0/ref/models/relations/ –

답변

2

아니요, 원하지 않습니다. 당신은 ForeignKey를 File에서 User로 바꾸고 싶습니다. 그러면 my_file.user.username에 액세스 할 수 있습니다.

참고 : 이와 같이 사용자 클래스를 정의하는 것은 바람직하지 않습니다. 이렇게하는 것이 좋은 이유가있을 수 있지만 그렇다면 인증 앱의 추상 기본 클래스를 상속해야합니다. 이렇게하지 않으면 암호를 일반 텍스트로 저장하므로 심각한 보안 문제가됩니다. 여기에 자신의 모델이 필요하지 않은 것처럼 보입니다. 이 클래스를 삭제해야합니다.