I am working on creating model in models.py file.
date_created = models.DateTimeField(null=False,default='0000-00-00 00:00:00')
above code working perfect as it create default in database as per mentioned.
concept_id = models.IntegerField(default=0,null=False)
above code not working as it can't able to create default value.
please help me for assign default value for above mentioned field.
Also How can i create Double Type using models.py ?? by using above code i am creating integer type field but can't create Double dataType.
Use a DecimalField. Ie
models.DecimalField(..., max_digits=5, decimal_places=2)
or a FloatField (Decimal is more precise, see https://docs.djangoproject.com/en/dev/ref/models/fields/#floatfield-vs-decimalfield).
Related
I am using the google cloud datastore python client to write an entity into the datastore which contains an embedded entity. An example entity might look like:
data_type: 1
raw_bytes: <unindexed blob>
values: <indexed embedded entity>
I checked the data from the console and the data is getting saved correctly and the values are present.
Next, I need to run a query from a python app engine application. I have represented the above as the following entity in my app engine code:
class DataValues(ndb.Model):
param1 = ndb.BooleanProperty()
param2 = ndb.IntegerProperty()
param3 = ndb.IntegerProperty()
class MyEntity(ndb.Expando):
data_type = ndb.IntegerProperty(required=True)
raw_bytes = ndb.BlobProperty()
values = ndb.StructuredProperty(DataValues)
One of the filters in the query depends on a property in values. Sample query code is as below:
MyEntity.query().filter(MyEntity.data_type == 1).filter(MyEntity.values.param1 == True).get()
I have created the corresponding composite index in my index.yaml
The query runs successfully but the resulting entity contains the embedded entity values as None. All other property values are present.
What can be the issue here ?
Add properties of DataValues entity as properties of the MyEntity.
This is a bit of a guess, but since datastore attributes are kind of keyed by both their name (in this case values) and the name of the "field type/class" (i.e. StructuredProperty), this might fix your problem:
class EmbeddedProperty(ndb.StructuredProperty):
pass
class MyEntity(ndb.Expando):
data_type = ndb.IntegerProperty(required=True)
raw_bytes = ndb.BlobProperty()
values = EmbeddedProperty(DataValues)
Give it a shot and let me know if values starts coming back non-null.
I struggled with the same problem, wanting to convert the embedded entity into a Python dictionary. One possible solution, although not a very elegant one, is to use a GenericProperty:
class MyEntity(ndb.Model):
data_type = ndb.IntegerProperty(required=True)
raw_bytes = ndb.BlobProperty()
values = ndb.GenericProperty()
values will then be read as an "Expando" object: Expando(param1=False,...). You can access the individual values with values.param1, values.param2 etc. I would prefer having a custom model class, but this should do the job.
I have to work with a database containing columns with a dash in their name, as for example a-name. When converting the table with peewee, it converts it to an illegal character, with python complaining about a misplaced operator.
For a table with 2 columns, id and a-name, the result would be
from peewee import *
database = MySQLDatabase('databasename', **{'password': 'pwd', 'host': 'ip', 'user': 'username'})
class BaseModel(Model):
class Meta:
database = database
class ATable(BaseModel):
id = PrimaryKeyField()
a-name = CharField()
class Meta:
db_table = 'aTable'
I found a temporary workaround by changing the dash to an underscore and using the optional parameter db_column, like
a_name = CharField(db_column='a-name')
Is there another possibility for this issue as I do not want to do manual changes everytime I download the models from the database server?
I should add that I have no control over the database server, I have merely an account with read-only permissions.
Greetings,
Luc
a_name = CharField(db_column='a-name')
This is the correct way to solve the problem. Python does not allow dashes in identifiers, so if your column uses them then specify the column name explicitly and use a nice name for the column.
I suppose you could look into modifying the playhouse.reflection.Introspector.make_column_name method, as well.
Hi need your help to understand some feature in CakePHP.
I have a SQL Table : user.
I generate with bake the Model : UserTable.
In the action home() of my UsersController, i have this :
$t_Results = TableRegistry::get('User')->findByLogin('jdupont')->execute()->fetchAll('assoc');
debug($t_Results);
The query is generated by Cake and this code works well.
My question are :
Must i create the function findByLogin inside the Model or not ?
Is my code correct ?
Thanks for the help ;)
Yes you can create a findByLogin in your model but you don't have to.
Your code works but doesn't respect conventions.
In CakePHP 3
SQL tables are singular lowercase,
Table files has upper first letter and plural suffixed by Table,
Controllers are plural first letter upper and suffixed by Controller.
If you follow these conventions in your controller you can do this:
$t_Results = $this->Users->findByLogin('jdupont')->execute()->fetchAll('assoc');
debug($t_Results);
You don't have to use ->execute(). Query objects are lazily evaluated, execute will be called when you will use the request.
One of the quickest ways for you to check if your code is correct is to actually run it and see if it returns what you expect.
findByLogin() is a Cake dynamic finder so you don't need to define this method as Cake dynamically does this for you. You can prefix any camel-cased column name with findBy to query a table using that column.
You can use it like this:-
$t_Results = $this->Users->findByLogin('jdupont')->first();
This is my code:
$get_all = Geo_Postal_us::findOrFail($postal);
With this query, Laravel tries to find the id field in the table.
I don't have an id field. I use the postal column as primary key.
How do you set the function to find value in postal column and not search from id column?
Thanks,
You could create the behaviour you are looking for with the following:
Geo_Postal_us::where('postal', $postal)->firstOrFail();
Laravel by default is searching for the "id" column inside the table if you are using find(). To avoid running into errors here and maybe later, you should always tell laravel that you did take another name for your primary field.
To do that, in your Geo_Postal_us just edit as the following:
class Geo_Postal_us extends Model
{
protected $primaryKey = 'postal'; //tell laravel to use 'postal' as primary key field
...
...
I ran into this issue within Voyager and it really drove me nuts :).
Hope this helps some people as they google the issue.
In my database model, my attribute is set as type INT.
On the front end, I want to display a select field with representative values for the respective Integer values.
eg: [1 = Home, 2 = About]
I am currently using an external plugin for the administrating content, and the select values only allows integer. So my idea is to achieve this at respective Model. Is it possible?
Genarally yes.
You should be able to attach results of Model->find('list') to select field. Of course your model should have name or title fields for description values (Home, About).
Sounds like the kind of enum representation as I always use.
Try this solution:
http://www.dereuromark.de/2010/06/24/static-enums-or-semihardcoded-attributes/
I basically uses an array matching to resolve those ints into strings in a clean way - using the model. can be the whole array for select fields or just the specific string for output in the view/index.
Its also fully form and bake-template capable.
If you name the field "attribute" in your table, and name the method "attributes()" you can easily have "cake bake" to bake this via custom templates.