i can't do makemigrations in django 1.11 (fields.E312) - python-3.6

class Player(models.Model):
name = models.CharField(max_length = 256)
number = models.IntegerField()
age = models.IntegerField()
is_captain = models.BooleanField(default = False)
class Injuries(models.Model):
player = models.ForeignKey(Player, Player.name)
team = models.ForeignKey(Team)

Below are my suggestions, as per my capability.
As per the documentation,
A many-to-one relationship. Requires two positional arguments: the
class to which the model is related and the on_delete option.
See the reference here. So the first positional argument is the class which is related by Foreign Key and the second position argument is on_delete. You should define it as models.CASCADE or as appropriate for your app.
Thus in this case the second positional argument is "Player.name". I think you have to first replace it with models.CASCADE.
Thus change the code from what is below
class Injuries(models.Model):
player = models.ForeignKey(Player, Player.name)
team = models.ForeignKey(Team)
to the one below
class Injuries(models.Model):
player = models.ForeignKey(Player, on_delete=models.CASCADE)
team = models.ForeignKey(Team)
The foreign key is set to the primary key of the related model. In this case (since you have not defined it), it will be player_id that is automatically assigned by django. If you want it to force it to use the "name" column from the Player model, then you have to set "unique=True" in the name field of the Player model. Also notice the use of to_field='name' option.
In such a case the changed code will be as below.
class Player(models.Model):
name = models.CharField(max_length = 256, unique=True)
number = models.IntegerField()
age = models.IntegerField()
is_captain = models.BooleanField(default = False)
class Injuries(models.Model):
player = models.ForeignKey(Player, to_field='name', on_delete=models.CASCADE)
team = models.ForeignKey(Team)
Hopefully it will work. If there is still an error, please let me know.

Related

How can I adapt this Power query recursion to handle multiple Parents in a hierarchy?

I'm trying to adapt the recursion code below, to handle the situation where there can be multiple ParentId for each Id (This is where I found the code on SO).
I see that the List.PositionOf can take an optional parameter with Occurence. All so that instead of returning the position of the Id in the List, I can return a list of the positions of all the matching Ids.
The problem I have is what to do next.
How can I use this list of positions to get the list of ParentId elements in the ParentID_List corresponding to these positions? Or is there a better approach?
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
ChangedType = Table.TransformColumnTypes(Source,{{"ID", type text}, {"ParentID", type text}}),
ID_List = List.Buffer( ChangedType[ID] ),
ParentID_List = List.Buffer( ChangedType[ParentID] ),
Type_List = List.Buffer( ChangedType[Type] ),
Highest = (n as text, searchfor as text) as text =>
let
Spot = List.PositionOf( ID_List, n ),
ThisType = Type_List{Spot},
Parent_ID = ParentID_List{Spot}
in if Parent_ID = null or ThisType=searchfor then ID_List{Spot} else #Highest(Parent_ID,searchfor),
FinalTable = Table.AddColumn( ChangedType, "StrategyID", each Highest( [ID],"Strategy" ), type text),
FinalTable2 = Table.AddColumn( FinalTable, "SubstrategyID", each Highest( [ID],"Substrategy" ), type text),
#"Replaced Errors" = Table.ReplaceErrorValues(FinalTable2, {{"SubstrategyID", null}})
in #"Replaced Errors"

How to trade multiple equities at once using Ibrokers in R

Using the Ibrokers account I know how to place a trade with one ticker which , in the example below I place a trade with "DAL"
library(IBrokers)
tws=twsConnect(clientId = 1,host = "localhost", port = 7497 )
contract=twsEquity(symbol = "DAL", exch = "SMART" )
order=twsOrder(action = "BUY", totalQuantity ="10", tif = "OPG" )
placeOrder(twsconn = tws, Contract = contract, Order = order)
However I am interested in trading multiple tickers at once for example how can I place an order to buy "DAL" and "AAL". How can I put multiple orders into IBrokers in R?
I doubt your code even works now. To place another order just place another order. Note that they must have separate ids and the ids have to increase every order.
library(IBrokers)
tws=twsConnect(clientId = 1,host = "localhost", port = 7497 )
id <- tws.reqids(1)
contract=twsEquity(symbol = "AAL", exch = "SMART" )
# give an order id
order=twsOrder(id, action = "BUY", totalQuantity ="10", tif = "OPG" )
placeOrder(twsconn = tws, Contract = contract, Order = order)
# increment id
id <- id+1
contract=twsEquity(symbol = "DAL", exch = "SMART" )
order=twsOrder(id,action = "BUY", totalQuantity ="10", tif = "OPG" )
placeOrder(twsconn = tws, Contract = contract, Order = order)
I don't think that code will work. The defaults for order look to be LMT with a price of 0. You may want to try orderType = "MKT". Check the documentation for IBrokers.
I would suggest using a different API, it doesn't look like you have used the R package much so it's not a big deal to switch. It probably won't work for much longer. I think it is unsupported and the API has changed since the last update.

How do I perform an NDB query on the properties of models that have a parent-child relationship?

So I have a root model that looks like this:
class Contact(ndb.Model):
first_name= ndb.StringProperty()
last_name= ndb.StringProperty()
age = ndb.IntegerProperty()
and a child model that looks like this:
class Address(ndb.Model)
address_type=ndb.StringProperty(choices=['Home','Office','School'],default='Home')
street = ndb.StringProperty()
city = ndb.StringProperty()
state = ndb.StringProperty()
I want to be able to perform a query similar to this:
Select first_name, last_name, street, city, state WHERE contact.age > 25 and address.city = 'Miami' and address_type = 'School'
I know I can perform searches more easily if I were to setup the addresses as a structured property within the contact model, but I don't like using Structured Properties because they don't have their own keys, thus making entity maintenance more challenging.
I tried doing a search for contacts first and then feeding the resulting keys into a WHERE IN clause but it didn't work, example:
query1 = Contact.query(Contact.age>25).iter(keys_only = True)
query2 = Address.query(Address.city=='Miami', Address.address_type=='School',Address.ancestor.IN(query1))
Any ideas as to how to go about this would be appreciated.
OK so it looks like my original idea of filtering one query by passing in the keys of another will work. The problem is that you can't perform a WHERE-IN clause against an ancestor property so you have to store the parent key as a standard ndb.KeyProperty() inside of the child entity, then perform the WHERE-IN clause against that KeyProperty field.
Here's an example that will work directly from the interactive console in the Appengine SDK:
from google.appengine.ext import ndb
class Contact(ndb.Model):
first_name= ndb.StringProperty()
last_name= ndb.StringProperty()
age = ndb.IntegerProperty()
class Address(ndb.Model):
address_type=ndb.StringProperty(choices=['Home','Office','School'],default='Home')
street = ndb.StringProperty()
city = ndb.StringProperty()
state = ndb.StringProperty()
contact = ndb.KeyProperty()
# Contact 1
contact1 = Contact(first_name='Homer', last_name='Simpson', age=45)
contact1_result = contact1.put()
contact1_address1 = Address(address_type='Home',street='742 Evergreen Terrace', city='Springfield', state='Illinois', contact=contact1_result, parent=contact1_result)
contact1_address1.put()
contact1_address2 = Address(address_type='Office',street=' 1 Industry Row', city='Springfield', state='Illinois', contact=contact1_result, parent=contact1_result)
contact1_address2.put()
# Contact 2
contact2 = Contact(first_name='Peter', last_name='Griffan', age=42)
contact2_result = contact2.put()
contact2_address1 = Address(address_type='Home',street='31 Spooner Street', city='Quahog', state='Rhode Island', contact=contact2_result, parent=contact2_result)
contact2_address1.put()
# This gets the keys of all the contacts that are over the age of 25
qry1 = Contact.query(Contact.age>25).fetch(keys_only=True)
# This query gets all addresses of type 'Home' where the contacts are in the result set of qry1
qry2 = Address.query(Address.address_type=='Home').filter(Address.contact.IN(qry1))
for item in qry2:
print 'Contact: %s,%s,%s,%s'% (item.contact.get().first_name, item.contact.get().last_name, item.address_type, item.street)
This will render a result that looks kinda like this:
Contact: Peter,Griffan,Home,31 Spooner Street
Contact: Homer,Simpson,Home,742 Evergreen Terrace
Can you use an Ancestor query?
query1 = Contact.query(Contact.age>25).iter(keys_only = True)
for contact in query1:
query2 = Address.query(Address.city=='Miami',
Address.address_type=='School',
ancestor=contact)
If that's not efficient enough, how about filtering the addresses?
query1 = Contact.query(Contact.age>25).iter(keys_only = True)
contacts = set(query1)
query2 = Address.query(Address.city=='Miami', Address.address_type=='School')
addresses = [address for address in query2 if address.key.parent() in contacts]

Add, Update, Delete from a ndb.KeyProperty() - Google Cloud Datastore NDB

Here are my many-to-many relationship models:
class ModelA(ndb.Model):
name = ndb.StringProperty(required=true)
model_b = ndb.KeyProperty(kind=ModelB,repeated=True)
class ModelB(ndb.Model):
name = ndb.StringProperty(required=true)
model_a = ndb.KeyProperty(kind=ModelA,repeated=True)
My question is, how do I add/update/delete a single (or many) KeyProperty from let's say model_b?
I managed to do it like this:
pos = ModelA.model_b.index(ndb.Key('ModelB',213)) # Get position from list
ModelA.model_b.pop(pos) # Remove from list
ModelA.put() # Update

Cassandra - CqlEngine - using collection

I want to know how I can work with collection in cqlengine
I can insert value to list but just one value so I can't append some value to my list
I want to do this:
In CQL3:
UPDATE users
SET top_places = [ 'the shire' ] + top_places WHERE user_id = 'frodo';
In CqlEngine:
connection.setup(['127.0.0.1:9160'])
TestModel.create(id=1,field1 = [2])
this code will add 2 to my list but when I insert new value it replace by old value in list.
The only help in Cqlengine :
https://cqlengine.readthedocs.org/en/latest/topics/columns.html#collection-type-columns
And I want to know that how I can Read collection field by cqlengine.
Is it an dictionary in my django project? how I can use it?!!
Please help.
Thanks
Looking at your example it's a list.
Given a table based on the Cassandra CQL documentation:
CREATE TABLE plays (
id text PRIMARY KEY,
game text,
players int,
scores list<int>
)
You have to declare model like this:
class Plays(Model):
id = columns.Text(primary_key=True)
game = columns.Text()
players = columns.Integer()
scores = columns.List(columns.Integer())
You can create a new entry like this (omitting the code how to connect):
Plays.create(id = '123-afde', game = 'quake', players = 3, scores = [1, 2, 3])
Then to update the list of scores one does:
play = Plays.objects.filter(id = '123-afde').get()
play.scores.append(20) # <- this will add a new entry at the end of the list
play.save() # <- this will propagate the update to Cassandra - don't forget it
Now if you query your data with the CQL client you should see new values:
id | game | players | scores
----------+-------+---------+---------------
123-afde | quake | 3 | [1, 2, 3, 20]
To get the values in python you can simply use an index of an array:
print "Length is %(len)s and 3rd element is %(val)d" %\
{ "len" : len(play.scores), "val": play.scores[2] }

Resources