I'm struggling getting the right query for my project. Here is an example or my model :
class OrderItem(models.Model):
order_type = models.CharField(max_length=50, blank=True, null=True)
certification_no = models.CharField(max_length=10, default="43534")
client = models.ForeignKey(Client, blank=True, null=True)
item = models.CharField(max_length=500)
quantity = models.PositiveIntegerField(default=1)
rate = models.DecimalField(decimal_places=2, max_digits=7)
signer = models.CharField(max_length=500)
item_value = models.DecimalField(decimal_places=2, max_digits=7)
certification_type = models.CharField(max_length=200)
packing_list = models.FileField(upload_to='files/%Y-%m-%d', blank=True, null=True)
wholesale = models.BooleanField(default=False)
auth_fee = models.DecimalField(decimal_places=2, max_digits=7, blank=True, null=True)
label_position = models.CharField(max_length=250)
order = models.ForeignKey('Order', blank=True, null=True)
ordered = models.BooleanField(default=False)
created_on = models.DateTimeField(auto_now_add = True)
def __unicode__(self):
return self.item
def total_auth_fee(self):
return self.quantity * self.auth_fee
class Meta:
ordering = ['-created_on']
class Client(models.Model):
user = models.OneToOneField(User)
phone_number = models.PositiveIntegerField(max_length=30,blank=False)
shipping_address = models.CharField(max_length=500, null=True)
zipcode = models.CharField(max_length=30,blank=False)
class Meta:
verbose_name_plural = "Client"
def __unicode__(self):
return self.user.username
How do I get phone_number, (name of the client and email address also) is in inbuilt django forms? Thank you
in you HTML code you can access User information easily. for example
<p>{{ user.username }}</p>
<p>{{ user.email }}</p>
<p>{{ user.first_name }}</p>
<p>{{ user.last_name }}</p>
and if you want to access them in a Django form, just add from django.contrib.auth.models import User
first of your code
for example look at this code. it's a simple form for add a new user
views.py
def Signup(request):
if request.method == "POST":
if not request.user.is_authenticated():
form = UserCreationForm(request.POST)
if form.is_valid():
new_username = form.cleaned_data['username']
new_password = form.cleaned_data['password1']
new_email = form.cleaned_data['email']
new_first_name = form.cleaned_data['first_name']
new_last_name = form.cleaned_data['last_name']
#hashed_password = hashlib.md5(new_password).hexdigest()
new_user = User.objects.create_user(
username = new_username,
email = new_email,
first_name = new_first_name,
last_name = new_last_name)
if new_password:
new_user.set_password(new_password)
else:
new_user.set_unusable_password()
new_user.save()
new_user.is_active = True
auth_user = authenticate(username = new_username, password = new_password)
if auth_user is not None:
login(request, auth_user)
return HttpResponseRedirect('../')
else:
form = UserCreationForm()
return render(request, 'Dashboard/signup.html', {'form': form})
forms.py
from django.contrib.auth.forms import UserCreationForm
class SignupForm1(UserCreationForm):
class Meta:
model = User
fields = ('username', 'password', 'email', 'first_name' , 'last_name')
if you look at forms.py
the SignupForm1
inherit from User
then you can have the user information ( fields )
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.