When I change the last value of the model in my list to false, that value changes to false everywhere, I want my value to be true in the first list element and remain false at the end.
How can I solve this problem?
mainAccount?.isMainAccount = true
checkedAccountList?.add(mainAccount!!)
mainAccount?.isMainAccount = false
checkedAccountList?.add(mainAccount!!)
Here you are changing property of same object that is why after third line your both item in list have false as mainAccount value. Take look at below diagram, In this I am trying to show that list item only contain reference to your object.
If you need to have separate value for both item than you must create new object and copy rest of values from first object and change mainAccount value to false.
for ex.
mainAccount?.isMainAccount = true
checkedAccountList?.add(mainAccount!!)
Account mainAccount2 = new MainAccount();
// copy other fields if needed
mainAccount2?.isMainAccount = false
checkedAccountList?.add(mainAccount2!!)
Related
I've come across the following line of code
_, created = UserProfile.objects.update_or_create(
What is the point of the "_"?
Thanks!
update_or_create returns a tuple. The first value is the object, the second value is a bool that is True if the object is created and False if it was updated.
https://docs.djangoproject.com/en/3.1/ref/models/querysets/
_ is a variable that is commonly used in python to denote this particular value is being ignored.
.update_or_create(…) [Django-doc] returns a 2-tuple:
Returns a tuple of (object, created), where object is the created or updated object and created is a boolean specifying whether a new object was created.
Here one uses iterable unpacking [pep-3132] to unpack the 2-tuple and assign the first and second value of the 2-tuple to a variable named _ and created respectively.
Often variables like _ and __ are used for values for which we are not interested. It is thus used as a "throwaway" variable that is necessary to unpack the 2-tuple.
Here likely the rest of the function is only interested in the value for created, not in the created or updated object.
I am looking for an example how to store one key and multiple values example qualifications as below:
[name:'Test1',job:'QA',qualifications:['Selenium','Java']]
Map<String,String>=new HashMap<String,String>//not allowing multiple values.
You need to know, that you are using a map containing entries of different key type. In your case, the most common key type for all values is Object, becuase name and job keys are of type String, while qualifications is of type List<String>. If you want to create such a map step by step (by specifying each key-value entry), you need to make sure that qualifications is a list. You can do it by assigning a list of predefined entries, or by assigning an empty list and then adding elements to it with << operator. Consider the following example:
def expected = [name:'Test1',job:'QA',qualifications:['Selenium','Java']]
def emp = [:]
emp.name = "Test1"
emp.job = "QA"
emp.qualifications = []
emp.qualifications << "Selenium"
emp.qualifications << "Java"
assert emp == expected
In this example, we create an empty list and later we add values to. Alternatively, we could assign a list containing both values already.
def expected = [name:'Test1',job:'QA',qualifications:['Selenium','Java']]
def emp = [:]
emp.name = "Test1"
emp.job = "QA"
emp.qualifications = ["Selenium", "Java"]
assert emp == expected
Your example from comment did something different. Instead of adding an element to a list, it was overriding emp.qualification entry with a value of type String. This is why it stored only single the last assigned value.
Initially I have inserted integer values hence schema created with column type number, later string values were also inserted in same column. Now I am facing issue while fetching values. Need tho update column type number to string.
Well, there are no columns in DynamoDB and even if you consider attributes as columns which they are not, they don't enforce specific type, except for primary key. Therefore you can't change the type of a column.
If you are asking about how to change type of a specific attribute for all items in a table, then you need to run update command on all of the items. DynamoDB unfortunately doesn't support batch update operation, therefore you need to fetch keys of all the items that you need to updated, loop through that list and update each item separately.
I recently had to do this. Here is my script that I used:
Assume that 'timestamp' is name of column you need to change from string to number. So here is solution:
import boto3
from boto3.dynamodb.conditions import Key
db_client = boto3.resource('dynamodb', region_name="eu-west-3")
table_res = db_client.Table(TABLE_NAME)
not_finished = True
ret = table_res.scan()
while not_finished:
for item in ret['Items']:
if 'timestamp' in item and isinstance(item['timestamp'], str):
new_item = item
new_item['timestamp'] = int(float(item['timestamp']))
print("fixing {}, {} -> {}".format(item['SK'], item['timestamp'], new_item['timestamp']))
table_res.put_item(Item = new_item)
if "LastEvaluatedKey" in ret:
last_key = ret['LastEvaluatedKey']
ret = table_res.scan(ExclusiveStartKey = last_key)
else:
not_finished = False
I do understand you probably don't need this anymore, but I still hope this will help somebody.
I've got a form with a set of checkboxes (all under the same field name) in my WebTest response, and I'd like to uncheck some of them according to their value. I've tried this:
my_form = response.forms['form-i-want']
for i in range(len(my_form.fields.get('field-i-want'))):
if my_form.fields.get('field-i-want')[i].value == "value-to-uncheck":
my_form.fields.get('field-i-want')[i].checked = False
Obviously this is very hacky looking code and there must be a better way. Also, this doesn't actually uncheck the box I want: when I then iterate through the checkboxes in the form there is no longer an element with the value value-i-want: the value has been set to None. And when I submit the form it behaves as if the nothing was done to the form.
Unfortunately your method for setting the checked status of the input will indeed have the unwanted side-effect of the input element being deleted.
As per the docs, to mark a checkbox input as being checked you will want to write:
my_form['field-i-want'] = True
Where 'field-i-want' is the value of the name attribute of the input element.
How should I search for all null values for a given object (name)?
model:
name = models.CharField(max_length=150,null=True,blank=True)
u_name=models.CharField(max_length=25,null=True,blank=True)
year=models.PositiveSmallInteger(blank=True,null=True)
I am looking to back populate some data through various 'lookups'.
It may or may not have value for various fields for a given object. I want to populate those fields that are null with the available values through lookups.
I can fetch one field that is null. But is there a way fetch all the null values for a give name 'rig'
u_name = Name.objects.filter(name='rig', u_name__isnull=True)
Thanks
Note that CharFields will store 'null' values as empty strings (https://docs.djangoproject.com/en/1.8/ref/models/fields/#null), so you may want to try the following:
u_name = Name.objects.filter(name='rig').filter(u_name=None)
or
u_name = Name.objects.filter(name='rig').filter(u_name="")
Update based on comments:
To get the Names with name='rig':
rigs = Name.objects.filter(name='rig')
then loop over empty u_names:
for u_name in rigs.filter(u_name=""):
# do things
and then loop over empty years:
for empty_year in rigs.filter(year=None):
# do things