Django를 처음 사용하므로 올바른 방향으로 가고 있지 않다면 알려주십시오. 저는 장고 프로젝트를 만들고 있는데 한 모델에서 데이터를 검색하여 다른 모델에서 사용하는 올바른 장고 방법을 묻고 싶습니다.Django - 외래 키를 사용하는 다른 모델의 데이터 참조
변수에 필수 필드를 할당하는 for 루프가 있지만 더 깨끗한 솔루션과 외래 키를 사용하는 솔루션이있는 경우이를 찾고있었습니다.
#MODELS
class Class1(models.Model):
tag_number = models.CharField(max_length = 300, null = True, blank = True)
example1 = models.CharField(max_length = 300, null = True, blank = True)
example2 = models.CharField(max_length = 300, null = True, blank = True)
class Class2(models.Model):
tag = models.ForeignKey(Class1, related_name = 'tag_foreignkey')
example3 = models.CharField(max_length = 300, null = True, blank = True)
example4 = models.CharField(max_length = 300, null = True, blank = True)
#VIEWS - This view is for Class2 but referencing fields from Class1
tag_number = str(instrumentform.cleaned_data['tag'])
query_results = Class1.objects.filter(tag_number = tag_number)
for query_result in query_results:
example5 = query_result.example1
example6 = query_result.example2
위의 작품 그러나 나는 일을 장고 방법이 아니다 및 외래 키를 활용하지 않는 가정
여기에 포함 된 코드의 예입니다.
누군가가 나를 올바른 방향으로 이끌어 줄 수 있다면 크게 감사 할 것입니다.
장고에서 ManyToMany 관계를 찾고있을 수도 있습니다. 설명서를 참조하십시오 : https://docs.djangoproject.com/en/dev/topics/db/examples/many_to_many/ – ruddra