I have this model of a course
class Minicursos(models.Model):
nome_minicurso = models.CharField(max_length=150)
resumo = models.TextField(max_length=500, default = "")
descricao = models.TextField(max_length=500, default = "")
imagem_minicurso = models.ImageField(upload_to="gallery", height_field=None,
width_field=None, max_length=None, default = "")
video_explicativo = models.FileField(upload_to="video/%y", default="",)
carga_horaria = models.IntegerField()
alunos = models.ManyToManyField(Profile, 'Alunos')
my html is like this
{% if user.is_authenticated %}
<form id="ingressar-form" method="POST">
{% csrf_token %}
{{ form.as_p }}
<a href="/minicurso/{{minicurso.id}}/aulas.html"><button id="ingressar" type="submit" value="Create">INGRESSAR</button></a>
</form>
{% else %}
<strong><p>Para ingressar no minicurso faça <a href="/login">Login</a> ou <a href="/register">Registre-se</a>.</p></strong>
{% endif %}
I wish that when the user clicks on 'ingressar' his profile, it will be added to the field students (alunos)
i tried it
if request.method == 'POST':
aluno = Minicursos.alunos.add(User)
aluno.save()
but I get this error
aluno = Minicursos.alunos.add(User) AttributeError: 'ManyToManyDescriptor' object has no attribute 'add'
Someone can help me please!?
You can only add an object to an instance of a model, not to the class object. So you first need to create an instance of Minicursos
.
You can check the official documentation for a detailed explanation.