Context: I'm forcing my self to learn django, I already wrote a small php based website, so I'm basically porting over the pages and functions to learn how django works.
I have 2 models
from django.db import models
class Site(models.Model):
name = models.CharField(max_length=50, unique=True)
def __str__(self):
return self.name
class Combo(models.Model):
username = models.CharField(max_length=50)
password = models.CharField(max_length=50)
dead = models.BooleanField(default=False)
timestamp = models.DateTimeField(auto_now_add=True)
siteID = models.ForeignKey(Site, on_delete=models.PROTECT)
class Meta:
unique_together = ('username','siteID')
def __str__(self):
return f"{self.username}:{self.password}#{self.siteID.name}"
When creating a view, I want to get the Combo objects, but I want to sort them first by site name, then username.
I tried to create the view, but get errors about what fields I can order by Cannot resolve keyword 'Site' into field. Choices are: dead, id, password, siteID, siteID_id, timestamp, username
def current(request):
current = Combo.objects.filter(dead=False).order_by('Site__name','username')
return render(request, 'passwords/current.html',{'current':current})
Since I'm not necissarily entering the sites into the database in alphabetical order, ordering by siteID wouldn't be useful. Looking for some help to figure out how to return back the list of Combo objects ordered by the Site name object then the username.
You can order this by siteID__name:
def current(request):
current = Combo.objects.filter(dead=False).order_by('siteID__name','username')
return render(request, 'passwords/current.html',{'current':current})
since that is the name of the ForeignKey. But that being said, normally ForeignKeys are not given names that end with an ID, since Django already adds an _id suffix at the end for the database field.
Normally one uses:
class Combo(models.Model):
# …
site = models.ForeignKey(Site, on_delete=models.PROTECT)
if you want to give the database column a different name, you can specify that with the db_column=… parameter [Django-doc]:
class Combo(models.Model):
# …
site = models.ForeignKey(
Site,
on_delete=models.PROTECT,
db_column='siteID'
)
Related
Basically, I am working on a django project, and whenever I insert data into the database, the result is weirdly formatted.
this is my model
customer.py
class Customer(models.Model):
user = models.OneToOneField(User,null=True,blank=True,on_delete=models.CASCADE)
name = models.CharField(max_length=200, null=True)
email= models.CharField(max_length=200, null=True)
phone_number= models.CharField(max_length=200, null=True)
def __str__(self):
return self.name
Now, say I have saved a new customer
new_customer = Customer.objects.create(name="Henry",email="henry#mail.com",phone_number="+330145786259")
new_customer.save()
when i try to retrieve the customer name i get this:
print(new_customer.name)
>('henry',)
Anyone has any insight for me???
I tried to recreate the model on a new project but still having the same result
In your customer class, you have defined a 1:1 relationship with the in-built user model class of django. And when you are creating the customer object, new_customer, you have not specified the 'user' attribute; hence, your customer object is missing a key element.
The user object already has an in-built field for storing names. It is 'first_name' and 'last_name.' You need to create a user model first before being able to create your 'Customer' model.
Your models.py should look something like this:
from django.contrib.auth.models import User
class Customer(models.Model):
user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
phone_number= models.CharField(max_length=200, null=True)
def __str__(self):
return self.user.first_name
# to return email -> self.user.email
Now to create a 'Customer' object in view.py:
from django.contrib.auth.models import User
from .models import Customer
# create a user object
myuser = User.objects.create_user(username='john', email='jlennon#beatles.com', password='glass onion')
# pass the user object to the customer model
mycustomer = Customer.objects.create(user=myuser, phone_number=123456789)
# save the customer object
mycustomer.save()
Explore django ModelForms to define the user model as per your specifications, e.g, if you don't require your users to have passwords associated with them, etc.
After much testing, I realized why I was getting the weird output.
I was directly passing data from a form to the object creation method, like so:
data = json.loads(request.body)
new_customer = Customer.objects.create(name=data['name'],email="henry#mail.com",phone_number="+330145786259")
new_customer.save()
So assigning the received data to a variable before passing it to the object creation method seems to be the right way of doing things... At least, it is working for me.
I'm making one of my first django apps with sqlite database. I have some models like for example:
class Connection(models.Model):
routeID = models.ForeignKey(Route, on_delete=models.CASCADE)
activityStatus = models.BooleanField()
car = models.ForeignKey(Car, on_delete=models.CASCADE)
class Route(models.Model):
name = models.CharField(max_length=20)
and forms
class RouteForm(ModelForm):
class Meta:
model = Route
fields = ['name']
class ConnectionForm(ModelForm):
class Meta:
model = Connection
fields = ['routeID', 'activityStatus', 'car']
And in my website, in the url for adding new Connection, I have cascade list containing RouteIDs. And I'd like it to contain RouteName, not ID, so it would be easier to choose. How should I change my ConnectionForm, so I could still use foreign key to Route table, but see RouteName instead of RouteID?
For now it's looking like this, but I'd love to have list of RouteNames, while still adding to Connection table good foreign key, RouteID
Update the Route Model's __str__ method:
class Route(models.Model):
name = models.CharField(max_length=20)
def __str__(self):
return self.name
Because the __str__() method is called whenever you call str() on an object. Django uses str(obj) in a number of places like in Modelform. By default it returns id or pk that is why you were seeing ids in model form. So by overriding it with name, you will see the names appear in choice field. Please see the documentation for more details on this.
I have searched high and low and even in between and for some reason cannot come up with a clear answer...
I am using django1.9 and created this model:
class paymentInfo(models.Model):
"""
Model for storing payment info
- Username as ForeignKey from userToCard
- Store payment token
- Store last 4
- Store card/bank name
- Store bool value for Default method
"""
username = models.ForeignKey(User, db_column='username', on_delete=models.CASCADE)
token = models.CharField(max_length=10)
last_4 = models.IntegerField()
bank_name = models.CharField(max_length=50)
default = models.BooleanField(default=0)
class Meta: # meta class to define the table name
db_table = 'payment_methods'
verbose_name_plural = 'Payment Methods' # for the admin site display
ordering = ('username',)
def __str__(self):
# in __str__ you should return a value of type string
# so self.username changed to self.username.username
return self.username.username # value displayed in admin view
I have created some objects using some different usernames and want to filter out the paymentInfo objects by user.
When I store the object, the database stores the user pk under the username column instead of the actual username string. I am not sure why, but that is not my issue here.
My issue is when I am trying to filter out paymentInfo.objects using the username or the user pk. I cannot seem to filter it out and the error I normally get is thus: FieldError: Cannot resolve keyword 'username' into field. Choices are: bank_name, default, id, last_4, token
P.S. I am using MySQL
If I understood you right, you are trying to filter data by username from table User what is a foreign key. In this case, this should help
paymentInfo.objects.filter(username__name='John')
Thanks to the answers provided, I was able to work out a solution (mainly using #Aamir Adnan 's method.)
class paymentInfo(models.Model):
"""
Model for storing payment info
- Username as ForeignKey from userToCard
- Store payment token
- Store last 4
- Store card/bank name
- Store bool value for Default method
"""
user = models.ForeignKey(User, on_delete=models.CASCADE)
token = models.CharField(max_length=10)
last_4 = models.IntegerField()
bank_name = models.CharField(max_length=50)
default = models.BooleanField(default=0)
class Meta: # meta class to define the table name
db_table = 'payment_methods'
verbose_name_plural = 'Payment Methods' # for the admin site display
ordering = ('user',)
def __str__(self):
return self.user # value displayed in admin view
def __unicode__(self):
return '%s' % (self.user)
The new __unicode__ inside of my class was so that I did not receive this error anymore:
TypeError: coercing to Unicode: need string or buffer, User found
i read a lot of forms.
i want to edit userinformation, but the userinformation is existed of two models.
One model this:
class Tc(LoginUser):
link = models.CharField(max_length=100)
name = models.CharField(max_length=50, unique=True)
contact = models.OneToOneField(Contact, blank=True, null=True)
def __str__(self):
return self.name
And the second one:
class Contact(models.Model):
contact_id = models.AutoField(primary_key=True)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
def __str__(self):
return self.email
Now i want to edit "contact" field.
It should be controled, if a contact is existing, if not, so create a new. If there is an existing one, then update this.
My problem is that, that i must use two forms and i dont know how exactly.
You need to make queries : for handle contact and know about a contact existing you need Making queries
. as a hint for check about existing if you have one unique object of contact like check_name you can do it with following :
from models import Contact
contats=contact.objects.all()
for n in contacts:
if n.first_name == check_name()
#do something
else:
#do something
I have the following code:
class Item(models.Model):
name = models.CharField(max_length=100)
keywords = models.CharField(max_length=255)
type = models.ForeignKey(Type)
class Meta:
abstract = True
class Variant(models.Model):
test_field = models.CharField(max_length=255)
class Product(Item):
price = models.DecimalField(decimal_places=2, max_digits=8,null=True, blank=True)
brand = models.ForeignKey(Brand)
variant = models.ForeignKey(Variant)
def get_fields(self):
return [(field.name, field.value_to_string(self)) for field in Product._meta.fields]
def __unicode__(self):
return self.name
Im using Grappelli.
I want my Product to have multiple Variations. Should I use a manytomanyfield?
I want to be able to add Variants to my Product directly in the Admin. Now I get an empty dropwdown with no variants(because they doesnt exists).
I thought Django did this automatically when u specified a Foreign Key?
How can I get the Variant fields to display directly on my Product page in edit?
I've read someting about inline fields in Admin?
Well, it's the other way around :)
1/ Place the foreign key field in your Variant, not in your Product (what you describe is actually a OneToMany relationship).
2/ Link the Variant to your Product in the relative ProductAdmin in admin.py as an inline (i.e VariantInline).
See the docs for further informations : https://docs.djangoproject.com/en/1.6/ref/contrib/admin/#inlinemodeladmin-objects
Hope this helps !
Regards,