How do can I creating and use a custom fields in Django - python

I am new to django and building a kinda of a package (Shipments) based app in Django and I have these models,
class ShippingLocation(models.Model):
latitude = models.IntegerField()
longitude = models.IntegerField()
class Shipping (models.Model):
email = models.EmailField()
location = models.ForeignKey(ShippingLocation , default=None, on_delete=models.CASCADE )
class Package(models.Model):
name = models.CharField(max_length=30)
supplier = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING)
to = models.ForeignKey(Shipping, default=None, on_delete=models.CASCADE )
this work okay for now but I wonder if can be able to remove the ShippingLocation model and use a custom field instead of at the location field in the Shipping model? If yes how do I create custom fields and how do I implement them?
so I have something like
class Shipping (models.Model):
email = models.EmailField()
location = models.DictionaryField()
and I get rid of the ShippingLocationModel

This should be work:
delete the SQLite database
change the model
then run the migrate command

Related

Dynamic django choice field

I have 4 models: Products (the list of products: freezers, microwaves, tvs and pcs), ProductType (entertainment and home appliances), Credit (a credit is registered on each purchase) and PurchaseReason (the reason why the customer has bought the product).
The PurchaseReason depend on the productType, so the purchaseReason has a foreignKey field productType.
In addition, each credit has a product as foreignKey and a purchaseReason as foreignKey.
Also, I have the ProductReason field as a choice field in the credit model, and I want the options to be set dynamically based on the product field of the credit model.
I'm creating an API so I think this cant be handle with modelForms, but i'm not sure. The hard work would be with the serializers (DRF) and with the django-admin (specially this one because in my product the django admin plays an important role)
What would be the best approach to manage my models in Django?
Here are my models. In credit I'm not sure how to implemente the purchase reason:
class Credit(models.Model):
client = models.ForeignKey('clients.Client', on_delete=models.SET_NULL)
name = models.CharField(max_length=30, null=False, blank=True)
product = models.ForeignKey('product',on_delete=models.SET_NULL)
reason_purchase = models.ChoiceField(????)
class PurchaseReason(models.Model):
product_type = models.ForeignKey(product_type, on_delete=models.CASCADE)
reason = models.CharField(max_length=30, null=False, blank=True)
class ProductType(models.Model):
name = models.CharField(max_length=30, null=False, blank=False)
class Product(models.Model):
model = models.CharField(max_length=30, default=None, null=True)
product_type = models.ForeignKey(product_type, on_delete=models.CASCADE)
When we use the foreign key, we need to mention the model name of that particular model so that we can integrate that particular model in that model as a reference entity. Have a look at this example.
from django.db import models
class Reporter(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.EmailField()
def __str__(self):
return "%s %s" % (self.first_name, self.last_name)
class Article(models.Model):
headline = models.CharField(max_length=100)
pub_date = models.DateField()
reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE)
def __str__(self):
return self.headline
class Meta:
ordering = ['headline']
you've not mentioned the model name properly. it should be Product in place of 'product' in the Credit class, product field.
use this reference https://docs.djangoproject.com/en/3.2/topics/db/examples/many_to_one/
i think you should be able to use the Foreignkey field properly after this. Although, if you can't, you can share the actual objective. i will help you to write the correct model.
Best wishes :)

Django: Making a value in a model unique specific to a user

I have a model for a project:
class Project(models.Model):
name = models.CharField(max_length=200, unique=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
I want to make the project name a unique value per user but at the moment if user 1 creates a name of "project 1" then user 2 is unable use that project name.
What is the best way to go about this?
Thanks!
unique_together is probably what you are looking for.
class Project(models.Model):
name = models.CharField(max_length=200)
user = models.ForeignKey(User, on_delete=models.CASCADE)
class Meta:
unique_together = (('name', 'user'),)

Usertype models in Django

I'm being confused on where table how I insert my usertypes, I'm just a beginner on Django but surely Im been reading documentation in Django but I can't understand this one , The case is when I register new user there's must be choice what usertype should specify with this user either an admin or etc. but the problem is I think there is no relationship table from authuser even I create another table.slight similar to this problem resources link. For now I'm been thinking to create custom usertype field in authuser table, but when migrate it didn't show updated fields and also like this issue some people or user didn't touch or add any field in authuser table sample it is possible to insert usertype in auth_permission or other default table? Im just really confused of where table I can add my usertype that have a relationship to authuser. Is there any know or explain about this, thanks
Models
class AuthUser(models.Model):
password = models.CharField(max_length=128)
last_login = models.DateTimeField(blank=True, null=True)
is_superuser = models.IntegerField()
username = models.CharField(unique=True, max_length=150)
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
email = models.CharField(max_length=254)
is_staff = models.IntegerField()
is_active = models.IntegerField()
date_joined = models.DateTimeField()
usertype_id = usertype = models.OneToOneField(usertypes,on_delete=models.CASCADE,primary_key=True)
() //this is what i want to add
class Meta:
managed = False
db_table = 'auth_user'
class usertypes(models.Model):
usertype = models.CharField(max_length=264)
description = models.CharField(max_length=264)
status = models.CharField(max_length=264)
There are multiple ways how to do it. The way I recommned is to extend the existing user model.
from django.contrib.auth.models import User
class UserType(models.Model):
user_type = models.CharField(max_length=264)
description = models.CharField(max_length=264)
status = models.CharField(max_length=264)
class AppUser(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
type = models.ForeignKey(UserType, on_delete=models.CASCADE)
This will create 2 extra tables.
user_type where you will have UserTypes that you probably want to fill and
app_user where will be stored reference on django user record and its type
However, I think you should have a good reason why you do this. Django allows you to group users into the user groups, what should be exactly what you want.

How to create ForeignKey in Django that connects only part of foreign Queryset?

I'm creating backend for Kiosks and each Kiosk model has Location model connected. But some locations have flag 'disabled'. I'm trying to remove this Locations from dropdown menu in Django Admin -> Kiosk so user couldn't choose it while creating/updating Kiosk instance.
I see two potential ways to solve it:
Edit Django admin page for Kiosk so in dropdown menu he can see only filtered results
Create proxy model with filtered results that would be connected to Kiosk
I would be happy to hear your suggestions, criticism
Let's suggest that models look like this:
class Location(models.Model):
'''Location of Kiosks'''
class Meta:
verbose_name = "Location"
verbose_name_plural = "Locations"
def __str__(self):
return self.name
name = models.CharField('Name', max_length=60)
description = models.TextField('Description')
enabled = models.BooleanField('Location enabled', default=True)
class Kiosk(models.Model):
'''Kiosk unit'''
description = models.TextField('Description')
uuid = models.CharField('Kiosk ID', unique=True, max_length=30)
location = models.ForeignKey(to=Location, on_delete=models.CASCADE)
You can make use of the limit_choices_to=… parameter [Django-doc] to limit the number of options:
class Kiosk(models.Model):
'''Kiosk unit'''
description = models.TextField('Description')
uuid = models.CharField('Kiosk ID', unique=True, max_length=30)
location = models.ForeignKey(
Location,
on_delete=models.CASCADE,
limit_choices_to={'enabled': True}
)
You can pass limit_choices_to to your ForeignKey so that it's choices are filtered
location = models.ForeignKey(
Location,
on_delete=models.CASCADE,
limit_choices_to={'enabled': True},
)

Django ManyToMany Field with an already existing table

What I'm trying to achieve is, having model Person that is created and managed by Django have a ManyToMany field with model Property that was "created" using inspectdb and already exists in the database.
(Property contains Geographical data and cannot be managed or changed by Django)
When trying to migrate, it raises :
ValueError: Related model 'cadastroapp.Property' cannot be resolved
Full stack here
Worth nothing that I removed from the migration file the step to create model Property, since it already exists and AFAIK there's no way to tell Django this in the model Class
models.py (simplified) :
class Person(models.Model):
objectid = models.AutoField(primary_key=True)
properties = models.ManyToManyField(
'Property',
through = 'Person_Property',
)
class Meta:
db_table = 'django_person'
class Person_Property(models.Model):
cod_person = models.ForeignKey('Person', on_delete=models.CASCADE)
cod_property = models.ForeignKey('Property', on_delete=models.CASCADE)
class Meta:
db_table = 'django_person_property'
class Property(models.Model):
objectid = models.BigIntegerField(unique=True, primary_key=True)
created_user = models.CharField(max_length=765, blank=True, null=True)
created_date = models.DateTimeField(blank=True, null=True)
last_edited_user = models.CharField(max_length=765, blank=True, null=True)
last_edited_date = models.DateTimeField(blank=True, null=True)
shape = models.TextField(blank=True, null=True) # This field type is a guess. - ESRI Shape
class Meta:
managed = False
db_table = '"GEO"."PROPERTY"'
There are a couple errors in your models.py file.
When defining a Foreignkey or ManytoMany field, you don't want the model name to be in quotes.
Please change:
class Person(models.Model):
properties = models.ManyToManyField(
'Property',
through = 'Person_Property',
)
and
class Person_Property(models.Model):
cod_person = models.ForeignKey('Person', on_delete=models.CASCADE)
cod_property = models.ForeignKey('Property', on_delete=models.CASCADE)
to:
class Person(models.Model):
properties = models.ManyToManyField(
Property,
through = 'Person_Property',
)
and
class Person_Property(models.Model):
cod_person = models.ForeignKey(Person, on_delete=models.CASCADE)
cod_property = models.ForeignKey(Property, on_delete=models.CASCADE)
then delete your migration file cadastroapp.0006_auto_20161122_1533.
then run makemigrations and migrate again.
This may still not migrate without errors, but it will get us on the right track.
I think that you want to put the model name in quotes. In case you leave it without quotes you have to ensure that the model is defined before the ManyToMany field has been defined. So you will need to have first class Property and then class Person in your file. When you put model name as "Property" then you do not need to care about order of class definitions.

Categories