Django Model with math - django-models

Hey i have a problem with my django model.
i have one class like this:
class Test(model.Models):
EV_Nr = models.IntegerField(unique=True)
Factor = models.DecimalField(max_digits=5, decimal_places=2, blank=False)
class Polter(models.Model):
EV = models.ForeignKey(Test)
RM = models.DecimalField(max_digits=10, decimal_places=2, default=0)
FM = models.DecimalField(max_digits=10, decimal_places=2, default=0)
My Problem:
RM or FM has to be 0, if both >0, it's an error and can not be saved.
but for example: RM = 10 and FM = 0. It's ok.
-> and the model has to check for the Factor in my Test Model and to do the
following: FM = RM / Faktor(from Test Model) and save it in my databese.
How can i do this. After two days of searching I have no idea.
Thanks for your help

Related

Want to create referral program in django

I want to create referral program using django models, referral should be look like linkedIn connections "https://www.linkedin.com/help/linkedin/answer/a545636/your-network-and-degrees-of-connection?lang=en" like 1st degree and 2nd degree.
ex-
If A refer B and then B refer C then for A (B is 1st degree connection and C will be 2nd degree connection).
How can I implement this?
I wrote logic in models.py but not getting required results.
models.py
class SaturnReferral(models.Model):
SaturnUser = models.CharField(max_length=50)
SaturnEmail = models.CharField(max_length=50, default="null")
ReferralCode = models.CharField(max_length=7)
Upvote = models.PositiveIntegerField(default=0)
Downvote = models.PositiveIntegerField(default=0)
recommended_by = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True, related_name='ref_by')
def __unicode__(self):
return unicode(self.recommended_by.id)
def save(self, *args, **kwargs):
#first_degree_connections
main_user1=self.SaturnUser
if recommended_by:
referrer1 = self.recommended_by.USERNAME_FIELD
UserReferral.create.object(main_user=main_user1,first_degree_users=referrer1)
mydata2 = SaturnConnections.objects.filter(creator=main_user).values()
referred1 = mydata2.friend
for u in referred1:
UserReferral.create.object(main_user=main_user1,first_degree_users=u)
#second-degree connections
if recommended_by:
main_user1=self.recommended_by.USERNAME_FIELD
mydata2 = SaturnConnections.objects.filter(creator=main_user).values()
second_degree_from_referred = mydata2.friend
for u1 in second_degree_to_referred:
UserReferral1.create.object(main_user=self.SaturnUser,second_degree_users = u1)
for u in referred1:
main_user1=u
mydata2 = SaturnConnections.objects.filter(creator=main_user).values()
second_degree_to_referred = mydata2.friend
for u1 in second_degree_to_referred:
UserReferral1.create.object(main_user=self.SaturnUser,second_degree_users = u1)
super(SaturnReferral, self).save(*args, **kwargs)
#Saturn Connections
class UserReferral(models.Model): # store first-degree connections
main_user = models.CharField(max_length=50)
first_degree_users = models.CharField(max_length=50)
class UserReferral1(models.Model): # store second-degree connections
main_user = models.CharField(max_length=50)
second_degree_users = models.CharField(max_length=50)
class SaturnConnections(models.Model):
created = models.DateTimeField(auto_now_add=True, editable=False)
creator = models.CharField(max_length=15)
friend = models.CharField(max_length=15)

Creating a Django model with unique values for each user

I would like to create a site that helps users to remember meaning of certain words.
class Word(models.Model):
word = models.CharField(max_length=30, unique=True)
meaning = models.CharField(max_length=200)
memory_strength = models.FloatField()
user = models.ForeignKey(User)
I want each user to have individual (unique) value of memory_strength for every item of Word, while values of word and meaning would be the same for each and every user. How can I achieve that?
class Word(models.Model):
word = models.CharField(max_length=30, unique=True)
meaning = models.CharField(max_length=200)
class Memory(models.Model):
memory_strength = models.FloatField()
user = models.ForeignKey(User)
word = models.ForeignKey(Word)

error when i inherit from abstract models in django

First of all i have been reading a lot and i dont found the answer.
i have a django project and inside that project i have some apps, more especifically
DJANGO-PROJECT
*--------- app_base
*--------- app_equipment
*--------- app_customer
then app_base is the app for code that is common to the other apps, in my case i have a models.py file a class who has a class:
from django.db import models
from datetime import date
import project_name.utilies as ut
class BaseModel():
status = models.CharField( max_length = 1, choices = ut.STATUS_CHOICES, default = 1 )
created_date = models.DateField( default = date.today )
last_modified_date = models.DateField( null = True )
deleted_date = models.DateField( null = True )
createdby_id = models.IntegerField( default = 0 )
modifiedby_id = models.IntegerField( default = 0 )
class Meta:
abstract=True
then in app_equipment i have another models.py file and in that file i have the next class:
from django.db import models
from app_base.models import BaseModel
class Equipment(BaseModel):
name = models.CharField( max_length = 250 , default="")
image = models.CharField( max_length = 250 , default="" )
desc = models.TextField( default="")
horsepower = models.IntegerField( default = 0 )
fuelcapacity = models.IntegerField( default = 0 )
model = models.CharField( max_length = 250 , default="" )
year = models.IntegerField( default = 0 , )
purchasecost = models.IntegerField( default = 0 )
class Meta:
db_table = "equipment"
so equipment extends from BaseModel or that whats i read in django documentation the error is when i want to execute commmand makemigrations i get an error:
You are trying to add a non-nullable field 'id' to equipment without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows)
2) Quit, and let me add a default in models.py
and i dont know how to solve it, i'been trying put id fields, and nothing happens, some people say the error is to clear, but what part of the diferents files and app is clear?
the error is solved when i dont inherit the abstract model, but i want to usea all those properties in BaseModel in each table of the others models
thanks in advance guys

Why is my write quota reached with a simple admin bulk remove?

This weird case happened twice already in the last 2 days.
I used Datastore Admin to remove all entities, no more than 100, to later reā€“upload db using remote_api_shell but after the request the Datastore Write Operations reached the limit:
This is the first and the only operation I did since last 24h reset.
Also the error is reported in remote_api_shell when I try to put new entities.
Any advice welcome.
Edit:
Here the models, nothing huge...
class Brand(BaseModel):
'''
Brand class
`Marca` in Etax db
'''
name = db.StringProperty()
abbr = db.StringProperty()
def __repr__(self):
return ('<Brand {0} instance at {1}>'
.format(self.abbr.encode('utf-8'), hex(id(self))))
class Model(BaseModel):
'''
Model class
`Gamma` in Etax db
'''
name = db.StringProperty()
code = db.IntegerProperty()
brand = db.ReferenceProperty(Brand, collection_name='models')
def __repr__(self):
return ('<Model {0} instance at {1}>'
.format(self.code, hex(id(self))))
class TrimLevel(BaseModel):
'''
Trim Level class
`Modello` in Etax db
'''
name = db.StringProperty()
etax_code = db.IntegerProperty()
start_production_date = db.DateProperty()
end_production_date = db.DateProperty()
retail_buy_prices = db.ListProperty(int)
retail_sell_prices = db.ListProperty(int)
list_prices = db.ListProperty(int)
model = db.ReferenceProperty(Model, collection_name='trim_levels')
fuel_supply = db.StringProperty()
gear_shift = db.StringProperty()
gear_speeds = db.IntegerProperty()
doors = db.IntegerProperty()
seats = db.IntegerProperty()
kw = db.IntegerProperty()
def __repr__(self):
return ('<TrimLevel {0} instance at {1}>'
.format(self.etax_code, hex(id(self))))
If you look at billing docs, that a high-level delete takes several low-level write operations:
Entity Delete (per entity): 2 writes + 2 writes per indexed property value + 1 write per composite index value
So if 100 entity deletes used 50k write ops, it means that your every entity had 500 index entries.
This can happen when entity has large list properties or havs a compound index spanning multiple list properties (= exploding index)
Do you have any compound indexes defined? What properties does your entity have?

google datastore many to one references

So i have two model classes:
class Dog(db.model):
dogName = StringProperty()
dogBreed = StringProperty()
class Cat(db.model):
catName = StringProperty()
catBreed = StringProperty()
and then i have a third model class to hold all the pictures
class Images(db.model):
imageReference = ReferenceProperty(*Animal*, collection_name = 'allImages')
imageURL = StringProperty()
Animal is either a Dog or a Cat. Obviously this does not compile.
Now my question is: Is there a way I can put Cat pictures in with Dog pictures? Or do I need to create more models like this:
class DogImages(db.model):
imageReference = ReferenceProperty(Dog, collection_name = 'allImages')
imageURL = StringProperty()
class CatImages(db.model):
imageReference = ReferenceProperty(Cat, collection_name = 'allImages')
imageURL = StringProperty()
You could use PolyModel:
class Animal(polymodel.PolyModel):
name = db.StringProperty()
breed = db.StringProperty()
class Dog(Animal):
pass
class Cat(Animal):
pass
Now you can have a ReferenceProperty that references Animals, and either Dogs or Cats will be permitted.
However, you don't have any properties that are specific to each type of animal - why not just have a regular Animal model, add a property indicating what species it is, and skip the separate models entirely?

Resources