英文:
Django movie and director relation. Movie is not attached to the director
问题
class Movie(models.Model):
name = models.CharField(max_length=100)
category = models.CharField(max_length=100)
imdb = models.DecimalField(max_digits=2, decimal_places=1)
director = models.ForeignKey('Director', on_delete=models.DO_NOTHING)
class Director(models.Model):
name = models.CharField(max_length=150)
movies = models.ManyToManyField(Movie, related_name='movies', blank=True)
I am creating a new movie using django-admin page. For instance; django-admin creating movie
However, the movie is not automatically added to the director.
Director
I also want a movie to belong to only one director.
How can I do these? I will be very glad if you can help me. Thanks in advance.
英文:
I'm making a model, consisting of a movie and a director. When I create a movie, I want the movie to be automatically owned by the director I selected but it's not working.
class Movie(models.Model):
name = models.CharField(max_length=100)
category = models.CharField(max_length=100)
imdb = models.DecimalField(max_digits=2, decimal_places=1)
director = models.ForeignKey('Director', on_delete=models.DO_NOTHING)
class Meta:
verbose_name_plural = 'Movies'
def __str__(self):
return self.name
class Director(models.Model):
name = models.CharField(max_length=150)
movies = models.ManyToManyField(Movie, related_name='movies', blank=True)
class Meta:
verbose_name_plural = 'Directors'
def __str__(self):
return self.name
I am creating a new movie using django-admin page. for instance;django-admin creating movie
However the movie is not automatically added to the director.
Director
I also want a movie to belong to only one director.
How can I do these. I will be very glad if you can help me. Thanks in advance
答案1
得分: 0
以下是翻译好的内容:
这已经是没有需要额外关系的情况了。确实,您可以这样定义:
class Movie(models.Model):
name = models.CharField(max_length=100)
category = models.CharField(max_length=100)
imdb = models.DecimalField(max_digits=2, decimal_places=1)
director = models.ForeignKey(
'Director', on_delete=models.DO_NOTHING, related_name='movies'
)
class Meta:
verbose_name_plural = 'Movies'
def __str__(self):
return self.name
class Director(models.Model):
name = models.CharField(max_length=150)
class Meta:
verbose_name_plural = 'Directors'
def __str__(self):
return self.name
如果您将导演 D1
分配给电影 M1
,您可以使用以下方式查询该导演的电影:
D1.movies.all()
在模型管理中,您还可以通过 inline 方式显示电影,例如使用 TabularInline
:
from django.contrib import admin
class MovieInline(admin.TabularInline):
model = Movie
@admin.register(Director)
class DirectorAdmin(admin.ModelAdmin):
# ...
inlines = [
MovieInline,
]
英文:
That's already the case without any need for an extra relation. Indeed, you define this as:
<pre><code>class Movie(models.Model):
name = models.CharField(max_length=100)
category = models.CharField(max_length=100)
imdb = models.DecimalField(max_digits=2, decimal_places=1)
director = models.ForeignKey(
'Director', on_delete=models.DO_NOTHING, <b>related_name='movies'</b>
)
class Meta:
verbose_name_plural = 'Movies'
def __str__(self):
return self.name
class Director(models.Model):
name = models.CharField(max_length=150)
class Meta:
verbose_name_plural = 'Directors'
def __str__(self):
return self.name</code></pre>
If you then assign a director <code>D<sub>1</sub></code> to a move <code>M<sub>1</sub></code>, you can query the movies of the director with:
<pre><code>D<sub>1</sub><b>.movies</b>.all()</code></pre>
In the modeladmin you can also display the movies when editing a Director
through an inline, for example a TabularInline
<sup>[Django-doc]</sup>:
<pre><code>from django.contrib import admin
class <b>MovieInline</b>(admin.TabularInline):
model = Movie
@admin.register(Director)
class DirectorAdmin(admin.ModelAdmin):
# …
inlines = [
<b>MovieInline</b>,
]</code></pre>
答案2
得分: 0
以下是我的代码:
serializers.py
class MovieSerializer(serializers.ModelSerializer):
class Meta:
model = Movie
fields = '__all__'
read_only_fields = ['movie_id']
class DirectorSerializer(serializers.ModelSerializer):
class Meta:
model = Director
fields = '__all__'
views.py
class DirectorListCreateAPIView(generics.ListCreateAPIView):
queryset = Director.objects.all().order_by('id')
serializer_class = serializers.DirectorSerializer
permission_classes = [permissions.IsAuthenticated]
authentication_classes = [authentication.BasicAuthentication]
def get_queryset(self):
queryset = Director.objects.all()
name = self.request.query_params.get('name', None)
if name is not None:
queryset = queryset.filter(name=name)
return queryset
class MovieListAPIView(generics.ListCreateAPIView):
queryset = Movie.objects.all()
serializer_class = serializers.MovieSerializer
permission_classes = [permissions.IsAuthenticated]
authentication_classes = [authentication.BasicAuthentication]
filter_backends = [django_filters.DjangoFilterBackend, filters.OrderingFilter, filters.SearchFilter]
filterset_fields = ['category', 'director']
search_fields = ['category__name', 'director__name', 'name',]
ordering_fields = ['production_year', 'imdb', 'duration']
英文:
I did as you said. I created a new movie
But It is not attached here as a list named movies
Here are my codes:
serializers.py
class MovieSerializer(serializers.ModelSerializer):
class Meta:
model = Movie
fields = '__all__'
read_only_fields = ['movie_id']
class DirectorSerializer(serializers.ModelSerializer):
class Meta:
model = Director
fields = '__all__'
views.py
class DirectorListCreateAPIView(generics.ListCreateAPIView):
queryset = Director.objects.all().order_by('id')
serializer_class = serializers.DirectorSerializer
permission_classes = [permissions.IsAuthenticated]
authentication_classes = [authentication.BasicAuthentication]
def get_queryset(self):
queryset = Director.objects.all()
name = self.request.query_params.get('name', None)
if name is not None:
queryset = queryset.filter(name=name)
return queryset
class MovieListAPIView(generics.ListCreateAPIView):
queryset = Movie.objects.all()
serializer_class = serializers.MovieSerializer
permission_classes = [permissions.IsAuthenticated]
authentication_classes = [authentication.BasicAuthentication]
filter_backends = [django_filters.DjangoFilterBackend, filters.OrderingFilter, filters.SearchFilter]
filterset_fields = ['category', 'director']
search_fields = ['category__name', 'director__name', 'name',]
ordering_fields = ['production_year', 'imdb', 'duration']
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论