Catch id and name via form, both forenig key

Asked

Viewed 524 times

0

Basically the user enters the company profile and makes a comment and a rating. I need the company ID to be passed on this form and I get the ID or User Name I have a system that uses USER extended for User and Business. And I created a Comment table, which imports the User Id from the Trade table and the Name of the user table. But I’m not being able to view the company id or the name of the user who is commenting. Remembering that only registered users can comment

py.models

class Negocio(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    empresa = models.CharField(max_length=50, blank=False)

class Usuario(models.Model):

    nome = models.CharField(max_length=50, blank=False)
    sobrenome = models.CharField(max_length=50, blank=False)

class Comentario(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    nome = models.ForeignKey(Usuario, on_delete=models.CASCADE)
    comentario = models.CharField(max_length=255, blank=True)
    nota = models.CharField(max_length=255, choices=NOTA_CHOICES, blank=True)

py views.

def comentario_form(request):
    comentario = Comentario.objects.all()
    form = ComentarioForm()
    data = {'comentario': comentario, 'form': form}
    return render(request, 'profile_negocio/comentario.html', data)

def cadastro_comentario(request): 
    if request.method == 'POST':
        form = ComentarioForm(request.POST)
        if form.is_valid():
            user = form.save()
            form.instance.user = request.user
            form.instance.nome = request.user
            user.usuario.nome = form.cleaned_data.get('comentario')
            user.usuario.sobrenome = form.cleaned_data.get('nota')
            user.save()

            return redirect('sistema_perfil')
    else:
        form = UsuarioForm()
    return render(request, 'profile_negocio/comentario.html', {'form': form})

html commentary.

<form method="post" action="{% url 'cadastro_comentario' %}" 
    class="form-signin" enctype="multipart/form-data" id="form" name="form" validate>
      {% csrf_token %}
  <div class="form-row ">
      <div class="form-group col-md-6 text-center">
          {{ form.comentario| as_crispy_field}}
          <div class="form-group col-md-6 text-center">
              {{ form.nota| as_crispy_field}}
          </div>

      </div>
  </div>

  <input type="submit" class="btn btn-primary ">

profile html.

{% for comentario in comentarios %}
    <div class="row"> 

        <div class="col-md-12">
            {{ comentario.nome }} - Nota: {{ comentario.nota }} 
        </div>
        <div class="col-md-12">
            {{ comentario.comentario }}
        </div>
    </div>

{% endfor %}
  • 1

    I don’t understand what the question is, some problem in some part of the code? In what part of the code you need help?

  • In the view, I need you to automatically pass the id of the Business, which is the page where the user will comment and pass tbm the name of the user who is commenting

  • I’ll work on my graduation to make it clearer.

1 answer

1


Brother, I’ve given the code refactored using CBV, try ai:

#models.py
class Negocio(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    empresa = models.CharField(max_length=50, blank=False)

class Usuario(models.Model):
    nome = models.CharField(max_length=50, blank=False)
    sobrenome = models.CharField(max_length=50, blank=False)

class Comentario(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    nome = models.ForeignKey(Usuario, on_delete=models.CASCADE)
    empresa = models.ForeignKey(Negocio, on_delete=models.CASCADE)
    comentario = models.CharField(max_length=255, blank=True)
    nota = models.CharField(max_length=255, choices=NOTA_CHOICES, blank=True)

#forms.py
from django import forms

class ComentarioForm(forms.ModelForm):
    class Meta:
        model = Comentario
        fields = '__all__'

#views.py
from django.views import generic
from django.urls import reverse_lazy

class ComentarioFormView(generic.UpdateView):

    model = Comentario
    form_class = ComentarioForm
    template_name = 'profile_negocio/comentario.html'
    success_url = reverse_lazy('name_da_url_para_direcionar_em_caso_de_sucesso_s2')

    def Negocio(self):
        return Negocio.objects.all()


    def form_valid(self, form):
        form = form
        comentario = Comentario(
                user=self.request.user.username,
                nome=form.cleaned_data.get('nome'),
                empresa=form.cleaned_data.get('empresa'),
                comentario=form.cleaned_data.get('comentario'),
                nota=form.cleaned_data.get('nota'),
            )
        comentario.save()
        return super().form_valid(form)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.