I would need to set the value of the 'overtime_w' field as the default value for the 'overtime_w_edit' field without a related field.
overtime_w = fields.Float(compute='_compute_t_overtime_w')
overtime_w_edit = fields.Float()
Related
I am trying to use the modelchoicefield that Django provides; however, it is not behaving the way I expected.
I want the display to the end user to be a string value from my model (which works), but return a the primary key of the Model when the user submits the form (not the display value). I attempted to use the to_field_name="id" on the Django form and it does render the values correctly when the HTML is rendered, but when the user submits the form the value I get back is the display value, not the primary key I am expecting.
customer_edit_form.py
annual_income = forms.ModelChoiceField(label='Annual income', to_field_name="id", queryset=cust_income_range.objects.all(), empty_label='-- None --')
models.py
# Annual Income Model
class cust_income_range(models.Model):
id = models.IntegerField(primary_key=True)
range = models.CharField(max_length=50)
def __str__(self):
return self.range
customer_view.py
annual_income = form.cleaned_data['annual_income']
After a lot of troubleshooing, I discovered that you can do something like this in the view to get the ID value instead of the str value.
annual_income = form.cleaned_data['annual_income'].pk
I would like to have an entity as follows:
class EntitySharedLinkPermission(models.Model):
OFF = None
COMPANY_VIEW = "Company View"
COMPANY_EDIT = "Company Edit"
PUBLIC_VIEW = "Public View"
PUBLIC_EDIT = "Public Edit"
name = models.CharField(max_length=12, primary_key=True)
class Meta: db_table = 'entity_shared_link_permission'
However, I cannot have NULL as a primary key value here. What should I do here instead? One idea was to just remove the primary key on this table and have a unique key instead (no PK in the table) to get around this, but surely there must be a better solution.
Simply put, you can't have null as primary key column value. You should always supply non null value to the primary key. Also, don't go for unique, it just isn't the solution though it masquerades as being one. If you can't always supply non null value, introduce a new identity column to your table instead.
If you don't expect the list of items to change often, and the set is small, then it looks to me like you're trying to set up a "choices" field, for which Django already has nice support. Here's the example that the Django docs use, which you could easily adapt to your situation:
from django.db import models
class Student(models.Model):
FRESHMAN = 'FR'
SOPHOMORE = 'SO'
JUNIOR = 'JR'
SENIOR = 'SR'
YEAR_IN_SCHOOL_CHOICES = (
(FRESHMAN, 'Freshman'),
(SOPHOMORE, 'Sophomore'),
(JUNIOR, 'Junior'),
(SENIOR, 'Senior'),
)
year_in_school = models.CharField(
max_length=2,
choices=YEAR_IN_SCHOOL_CHOICES,
default=FRESHMAN,
)
def is_upperclass(self):
return self.year_in_school in (self.JUNIOR, self.SENIOR)
However, if you expect the list of permissions to be fluid and change often, you should consider making the permissions a Model of their own, and simply use a ForeignKey (or ManyToMany) relationship,
I am trying to save an instance of a model but get a ValueError.
ValueError: Cannot assign "<DataPointModel: DataPointModel object>":
"Bike.data_point" must be a "DataPointModel" instance.
The model has a very simple field:
data_point = models.ForeignKey(DataPointModel, null=True, blank=True)
And the method is as well.
data_point = DataPointModel.objects.filter(
object_id=bike.reference_model.id,
).last()
watch.data_point = data_point
watch.save()
Why is this not saving?
Whenever you update or create anything in the table contains foreign Key you need to pass the object of primary key instead of passing real value.So you have to call the get query to primary key value table then pass that obj to foreign key column as a value.
Example :-
Suppose I have two model as follows:-
class model1(models.Model):
name=models.CharField(primary_key=True,,max_length=2000)
className=models.CharField(max_length=2000,null=True)
class model2(models.Model):
name=models.ForeignKey(model1)
teacher=models.CharField(max_length=2000,null=True)
views.py:-
jimmy = model2.objects.get(name="Jimmy")
obj = model1.objects.get(name='Piyush')
model2.objects.filter(id=jimmy.id).update(teacher=obj)
I want to save data in models where fieldname stored in a variable but while storing it is giving error of invalid keyword argument
my code :
field = request.POST['creationLanguage']
title = Translation.objects.create(field = request.POST['title'])
Here field stores the field name for model Translation but how I store data with this dynamic field_name .
Use the kwargs magic:
field = request.POST['creationLanguage']
value = request.POST['title']
title = Translation.objects.create(**{field: value})
I've had some data being gathered in production for a couple of days with, lets say, the following model:
class Tags(ndb.Model):
dt_added = ndb.DateTimeProperty(auto_now_add=True)
s_name = ndb.StringProperty(required=True, indexed=True)
Imagine I now add a new property to the model:
class Foo(ndb.Model):
is_valid = ndb.BooleanProperty(default=False)
some_key = ndb.KeyProperty(repeated=True)
class Tags(ndb.Model):
dt_added = ndb.DateTimeProperty(auto_now_add=True)
name = ndb.StringProperty(required=True, indexed=True)
new_prop = ndb.StructuredProperty(Foo)
... and gather some more data with this new model.
So now I have a portion of data that has the property new_prop set, and another portion that does not have it set.
My question is: how to I query for the data with the new property new_prop NOT set?
I've tried:
query_tags = Tags.query(Tags.new_prop == None).fetch()
But does not seem to get the data without that property set... Any suggestions?
Thanks!
The Datastore distinguishes between an entity that does not possess a property and one that possesses the property with a null value (None).
It is not possible to query for entities that are specifically lacking a given property. One alternative is to define a fixed (modeled) property with a default value of None, then filter for entities with None as the value of that property.