variables used in django models are class variables or instance variables? - django-models

I am new to django. Can someone please solve my doubt? The variables used in django models are class variables or instance variables? if they are class variables, how each object can store an unique value like instance variable?
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=64)
price = models.DecimalField(decimal_places=2, max_digits=100)
description = models.TextField()
summary = models.TextField(blank=True, null=True)

It is instance variables.
Because its values are different in each object of the model and it is not shared between all objects.

Related

JSONField validation based on another model object

Im trying to build an online bicycleauction and i cant seem to figure this one out.
Class Product(models.Model):
name_of_bicycle = models.Charfield()
Class Bid(models.Model):
bid_number = models.PositiveIntegerField()
name_of_bidder = models.Charfield()
bid = models.JSONField()
The JSONField should contain [name_of_bicycle, bidding amount].
Is this possible? Do i have to use JSON schema?

Managing a ForeignKey Relation in Django Admin

I am using django to create an inventory management system. There are two models store and retailer. Each retailer can only work in one store, but each store can have many retailer s.
Therefore, store is a ForeignKey in my retailer model. I have registered models in django admin. When I add a retailer I can choose a store.
When I add a store, I would like to be able to add a retailer to a store in the same page I add a store.
class Retailer(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
phone_number = models.IntegerField(default=00000000000)
store = models.ForeignKey(Store,null=True,on_delete=models.CASCADE)
class Store(models.Model):
name = models.CharField(max_length=200)
serial = models.CharField(max_length=200)
number_of_visitors = models.IntegerField(default=0)
it is called inline models:
https://books.agiliq.com/projects/django-admin-cookbook/en/latest/edit_multiple_models.html
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#inlinemodeladmin-objects
And you can find a lot of material about,
But you need something like this:
on your admin.py
#Create a class for inline display:
class RetailerINLINE(admin.TabularInline): #or stacked inline
model = Retailer
extra = 1
class StoreAdmin(admin.ModelAdmin):
.....
inlines = RetailerINLINE,

Django Inline model frontend

I need to have four file fields for my model so i am using Django inline model for that. I also need to create a model for with all thode fields so that the user can fill out the form and i am using Class based views- CreateView for that. However i do not know how to get the file fields in my model form.
Models.py
class Product(models.Model)
name= models.Charfield(maxlength=50)
city= models.Charfield(maxlength=50)
state=model.Charfield(maxlength=50)
year=models.Integerfield(blank=True, null=True)
class ProductAttachment(models.Model)
attachment = models.ForeignKey(Product, related_name='attachment')
appendix_a= models.Filefield(verbose_name='Appendix A')
appendix_b= models.Filedield(verbose_name='Appendix B')
Other = models.Filefield()
Admin.py
class ProductInline(admin.StackedInline)
model=ProductAttachment
class ProductAdmin(admin.modelAdmin)
inlines= [ProductInline,]
model=Product
admin.site.register(Product, ProductAdmin)
forms.py
class ProductForm(models.form):
class Meta:
model=Product
fields='__all__'
Views.py
class ProductCreateView(CreateView):
model=Product
form_class = ProductForm
However i do not know how to get all the filefield in the form. Any help is highly appreciated. Thank you
Looking at your code the model for your ProductForm and ProductCreateView is Product, which does not contain any FileFiled. You need to create form/view for ProductAttachment as well.

Problems with StructuredProperty and StringProperty

i am doing the finally degree work in Google App Engine, but i am having problems when i try this:
class Predicate(ndb.Model):
name = ndb.StringProperty()
parameters = ndb.JsonProperty()
class State(ndb.Model):
predicates = ndb.StructuredProperty(Predicate, repeated=True)
class Action(ndb.Model):
name = ndb.StringProperty()
parameters = ndb.StringProperty(repeated=True)
preconditions = ndb.StructuredProperty(Predicate, repeated=True)
predicatesToAdd = ndb.StructuredProperty(Predicate, repeated=True)
predicatesToDel = ndb.StructuredProperty(Predicate, repeated=True)
class Plan(ndb.Model):
plan = ndb.StructuredProperty(Predicate, repeated=True)
class Problem(ndb.Model):
initialState = ndb.StructuredProperty(Predicate)
goalState = ndb.StructuredProperty(Predicate)
actions = ndb.StructuredProperty(Action, repeated=True)
i get this error:
TypeError: This StructuredProperty cannot use repeated=True because its model class (Predicate) contains repeated properties (directly or indirectly).
StructuredProperty, if it contains repetitions, can not be replicated another StructuredProperty. But I need this structure models. How can i solve this?
And sorry for my bad english :(
I solved this problem using LocalStructuredProperty, but I think it will not work at all
The problem with your design is that ndb does not allow nested repeated properties. In other words, you cannot have a repeated structured property, which in turn has its own repeated property. If you remove the repeated=True from the parameters property, it will work.
You will need to re-think your design to work around this. One possible solution may be to use a JsonProperty for parameters, and store the list of strings as a JSON string. You won't be able to query them then of course, but it may work out depending on your requirements.

Multiple references to the same model in another model in Django

Hi I have a lot of users in my system who are classified into different types. I want to store the address details of all those users. For instance the user could be a student, a school or a franchisee. All the users here could have an address information associated with them.
from django.db import models
from django.contrib.auth.Models import User
class Address(models.Model):
user = models.OneToOneField(User)
address = models.TextField()
class Student(models.Model):
user_id = models.ForeignKey(User)
address = models.ForeignKey(Address)
class School(models.Model):
user_id = models.ForeignKey(User)
address = models.ForeignKey(Address)
contact_person_name = models.CharField(max_length=50)
In this scenario there are 2 references to the User model - one through user_id and another through address.user though they should be referring to the same instance. Is it a bad practice to have duplicate references?
I thought of leaving out the 'user' foreignkey in Address, but I think that the address can't exist without a user. How to better model this?
As you already mentioned in question duplication of same field in
a model is not a good Idea.
If these are your actual models, I would suggest you using abstract
models:
from django.db import models
from django.contrib.auth.Models import User
class Profile(models.Model):
user = models.OneToOneField(User, related_name="%(app_label)s_%(class)s_related")
address = models.TextField()
class Meta:
abstract = True
class Student(Profile):
pass
class School(Profile):
contact_person_name = models.CharField(max_length=50)
This will generate two tables: students, schools with fields
user, address and user, address, contact_person_name
respectively.

Resources