Reputation: 697
I have a problem and I can't find the way to do it. I found this post but can't make it work. Create django object using a view with no form
I get the following error
AttributeError at /work_orders/new_order/ 'module' object has no attribute 'OrderMovements'
Well this is the code
models.py
from django.db import models
# Importamos los movimientos disponibles
from .choices import *
# Importaos Stores y Clients
from client.models import Client
from store.models import Store
# Create your models here.
class WorkOrder(models.Model):
fk_client = models.ForeignKey(Client)
fk_store = models.ForeignKey(Store)
sector = models.IntegerField(choices=SECTOR_CHOICES, default=1)
article = models.CharField(max_length=20)
serial = models.CharField(max_length=25)
work = models.CharField(max_length=20)
article_details = models.CharField(max_length=255)
cash_advance = models.DecimalField(max_digits=6, decimal_places=2,
default=0)
initial_price = models.DecimalField(max_digits=6, decimal_places=2,
default=0)
randpassw = models.CharField(default='12345', max_length=5, blank=True,
null=True)
warranty = models.PositiveSmallIntegerField(default=0, blank=True,
null=True)
last_status = models.IntegerField(choices=STATUS_CHOICES, default=1)
def get_id(self, obj):
return obj.id
class OrderMovements(models.Model):
fk_workorder = models.ForeignKey(WorkOrder)
status = models.IntegerField(choices=STATUS_CHOICES, default=1)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
def __str__(self):
return self.status
and the view in views.py
def new_order(request):
if not request.user.is_authenticated():
return redirect('auth_login')
title = "Nueva Orden de trabajo"
store = request.user.userprofile.fk_store
if 'dni_cuit' in request.session:
dni_cuit = request.session.get('dni_cuit')
if Client.objects.get(dni_cuit=dni_cuit):
dni_cuit = request.session.pop('dni_cuit')
client = Client.objects.get(dni_cuit=dni_cuit)
form = NewOrderForm(request.POST or None,
initial={'fk_store': store,
'fk_client': client})
context = {
"title": title,
"form": form
}
else:
return redirect('new_client')
else:
form = NewOrderForm(request.POST or None,
initial={'fk_store': store})
context = {
"title": title,
"form": form
}
# Si el formulario es valido guardamos el contenido
if form.is_valid():
order = form.save(commit=False)
# Generamos el password aleatorio y lo guardamos en la instancia
randpassw = get_random_string(length=5, allowed_chars='abcdefghijclmnopqrstuvwxyz0123456789')
order.randpassw = randpassw
# Guardamos el nuevo registro (nueva orden de trabajo)
order.save()
# Tomamos el ID de la orden generada y lo guardamos en variable
order_last_id = WorkOrder.objects.latest('id')
# Si la orden fue generada en CuyoTek se guarda como recibida en Cuyotek
if str(store) == "CuyoTek":
# Status 3 es "Recibida en Cuyotek"
order_movement = models.OrderMovements(fk_workorder=order_last_id,
status=3)
# Si la orden no fue generada por un asociado de CuyoTek se registra
# como "En Tienda Asociada"
else:
order_movement = models.OrderMovements(fk_workorder=order_last_id,
status=2)
order_movement.save()
context = {
"title": "Orden Creada",
}
return render(request, "workorders/new_order.html", context)
What I have to do is this. When the user creates a new WorkOrder I have to save a new register in the model OrderMovements with the id of the workorder created. and the Status "3" if the store of the user is CuyoTek and "2" if not.
but this not work.
Upvotes: 1
Views: 1977
Reputation: 697
Here is the full views.py
from django.shortcuts import render, redirect
from .forms import *
from .models import *
from client.models import Client
# Importamos libreria para crear cadena aleatoria
from django.utils.crypto import get_random_string
# Create your views here.
def index(request):
if not request.user.is_authenticated():
return redirect('auth_login')
title = "Lista de Ordenes"
queryset = WorkOrder.objects.all()
context = {
'title': title,
'queryset': queryset,
}
return render(request, "workorders/index.html", context)
def new_order(request):
if not request.user.is_authenticated():
return redirect('auth_login')
title = "Nueva Orden de trabajo"
store = request.user.userprofile.fk_store
if 'dni_cuit' in request.session:
dni_cuit = request.session.get('dni_cuit')
if Client.objects.get(dni_cuit=dni_cuit):
dni_cuit = request.session.pop('dni_cuit')
client = Client.objects.get(dni_cuit=dni_cuit)
form = NewOrderForm(request.POST or None,
initial={'fk_store': store,
'fk_client': client})
context = {
"title": title,
"form": form
}
else:
return redirect('new_client')
else:
form = NewOrderForm(request.POST or None,
initial={'fk_store': store})
context = {
"title": title,
"form": form
}
# Si el formulario es valido guardamos el contenido
if form.is_valid():
order = form.save(commit=False)
# Generamos el password aleatorio y lo guardamos en la instancia
randpassw = get_random_string(length=5, allowed_chars='abcdefghijclm' +
'nopqrstuvwxyz' +
'0123456789')
order.randpassw = randpassw
# Guardamos el nuevo registro (nueva orden de trabajo)
order.save()
# Tomamos el ID de la orden generada y lo guardamos en variable
order_last_id = WorkOrder.objects.latest('id')
# Si la orden fue generada en CuyoTek se guarda como recibida en Cuyote
if str(store) == "CuyoTek":
# Status 3 es "Recibida en Cuyotek"
order_movement = models.OrderMovements(fk_workorder=order_last_id,
status=3)
# Si la orden no fue generada por un asociado de CuyoTek se registra
# como "En Tienda Asociada"
else:
order_movement = models.OrderMovements(fk_workorder=order_last_id,
status=2)
order_movement.save()
context = {
"title": "Orden Creada",
}
return render(request, "workorders/new_order.html", context)
Thanks, and thanks for the suggested edit, all help to gradually improve my poor English
Upvotes: 1