Прямое присвоение прямой стороне множества "многие ко многим" запрещено. Вместо этого используйте creator.set()
Я получаю эту ошибку: Прямое присвоение прямой стороне множества "многие ко многим" запрещено. Вместо этого используйте creator.set(). Новичок в django
Я полагаю, что есть ошибка с назначением моего метода create. Вот мои файлы: Любой совет поможет
// Views.py
def createTrip(request):
trip_creator = User.objects.get(id = request.session['loggedInID'])
newTrip = Trip.objects.create(
creator = trip_creator,
description = request.POST['description'],
city = request.POST['city'],
country = request.POST['country'],
photo = request.POST['photo']
)
print(newTrip)
return redirect('/home')
//models.py
class Trip(models.Model):
creator = models.ManyToManyField(User, related_name= "trips")
description = models.CharField(max_length= 255)
city = models.CharField(max_length= 255)
country = models.CharField(max_length= 255)
photo = models.ImageField(upload_to='static/img/trips')
// html
<div class="form-container">
<h4>Add a Trip</h4>
<form action="/createTrip" method="post" class="reg-form">
{% csrf_token %}
<p><input class="field" type="text" name="city" placeholder="City" id=""></p>
<p><input class="field" type="text" name="country" placeholder="Country" id=""></p>
<p><textarea name="description" id="" cols="30" rows="10"></textarea></p>
<p><input class="field" type="file" name="photo" placeholder="Photo" id=""></p>
<input class="form-btn" type="submit" value="submit">
</form>
</div>
//settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = 'static/img/trips/'
MEDIA_URL = os.path.join(BASE_DIR, 'static/img/trips/')