简体   繁体   中英

How to override UpdateView get_queryset django

I'm new to django, and I'm trying to make a simple update form where you can enter the PK of the object to be modified, and have the fields to be modified of the object in the same form, without entering the pk through the url, instead enter the PK from the form previously opened by the url http://127.0.0.1:8001/update/ , I am using UpdateView and I try to cancel the get_queryset method, so far what I have achieved is to cancel the method and open the form but I must pass a pk if not it doesn't open I don't know how to make the form open blank.

Model:

class Lote(models.Model):
  lote = models.CharField(verbose_name='Lote', max_length=10, primary_key=True)
  codigo = models.CharField(verbose_name='Codigo', max_length=10, blank=True, null=True)   
  producto = models.CharField(blank=True, max_length=50, null=True, verbose_name='Producto')
  timestamp=  models.DateTimeField(auto_now_add=True)

Forms:

class LoteForm(forms.ModelForm):    
    class Meta:
        model = Lote
        fields = ["lote","producto","codigo"]

class LoteFormView(forms.Form):
    lote = forms.CharField(max_length=20)
    producto = forms.CharField(max_length=20)
    codigo = forms.CharField(max_length=20) 

View:

class Update(UpdateView):
   model = Lote
   template_name = 'lote/updateview.html'
   fields = ['lote', 'codigo', 'producto']
   success_url = '/update/'

   def get_object(self, queryset=None):
        return Lote.objects.get(pk=11111)

url:

url(r'^update/$', Update.as_view(), name='update'),

Result:

enter image description here

what I want

enter image description here

I want to open the blank form and then pass the Lote(pk) to it and be able to update the other fields and redirect to the blank form

Thanks for your help and attention

the solution i got was

def updates_lote(request):
if request.method == 'POST':
    form = UpdatesLoteForm(request.POST)
    if form.is_valid():
        lote = form.cleaned_data['lote']
        serial = form.cleaned_data['serial']
        obj = Lote.objects.get(lote=lote)
        obj.serial = serial
        obj.save()           
        return HttpResponseRedirect('/updates_lote/')
else:
    form = UpdatesLoteForm()    
return render(request, 'lote/updates_lote.html', {'form': form})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM