Cannnot Assign- must be a instance Django - django

I have a table called job_position and there is a foreign key called region.
position =job_position.objects.get(id=position_id)
position.department= department.objects.get(department_name =request.POST.get("department")),
the "department" is passed from the HTML form, and the value of it is "Asia"
I am quite sure the error is from the second line
Cannot assign "(<region: Asia>,)": "job_position.region" must be a "region" instance.
But when I am trying the code in python shell ,it works fine,ie.e can get the region object and pass it to the position object region attribute and works fine. But somehow it does not works in the web development.
Anyone can help explain? Thanks very much

Cannot assign "(<region: Asia>,)"
As you have a comma at the end of your line, it turned it object into a tuple. Simply remove the comma:
position.department = department.objects.get(department_name=request.POST.get("department"))

Related

Including parameter expression

I'm new in Informatica PowerCenter, trying to use a new declared parameter expression in Workflow Session parameter file.
Objective: the expression is being feed automatically from data base, so the reason behind all this is to be able to change the logic in db, parameter file will re-generate in the next run with the new logic which ultimately will cause not to change nothing in power Center, only record expression in db. Off course for new fields you will need to change informatica.
Let me summarize or try to be deatailed as posible.
Parameter file has no issues, when executing the workflow it's being read with out issues, here's an example of the parameter expression I'm trying to use (rest of them, dbCon, time etc i delete them)
[Example.WF:Example.WT:.ST:Example]
$$Param_checker=IIF(1=1,'A','B')
Mapping
Created the parameter in the mapping as:
Name: $$Param_checker
Type: Parameter
Data Type: String (as per doc)
Precision: 10000 <-- making sure there's no cut
IsExprVar: true
Created a field in a Transformation Expression Box (the violet one) as:
Name: out_param_checker
Data Type: String
Precision: 10
O: X
Expresion: $$Param_checker
Workflow
In it session reads correctly the parameter file of the Mapping replaces all variables but when it comes to $$Param_checker it's not being expanded, this is a sample of log outcome when inserting on data base (cast and allfunctions are done by Inf. aut.:
CASE WHEN CAST('$$Param_checker' AS VARCHAR(4000))
And off course in Data base when process finishes I see the string as well '$$Param_checker'
Just in case, and sorry for being verbose, I do already have a parameter with a expression working but in a source Qualifier using it on a SQL statement.
The issue is why can't I use it in a out field. Forgot to mention that we have redshift under the hood, all sessions are set to "Full" Optimization, hence I think that's the issue.
If anyone have any hint would appreciate it.
Thanks

Get part of django model depending on a variable

My code
PriceListItem.objects.get(id=tarif_id).price_eur
In my settings.py
CURRENCY='eur'
My Question:
I would like to pick the different info depending on the CURRENCY variable in settings.py
Example:
PriceListItem.objects.get(id=tarif_id).price_+settings.CURRENCY
Is it possible?
Sure. This has nothing to do with Django actually. You can reach the instance's attribute through pure Python:
getattr(PriceListItem.objects.get(id=tarif_id), 'price_'+settings.CURRENCY)
Note it might be a better idea to have a method on the model which accepts the currency as a parameter and returns the correct piece of data (through the line I wrote above, for example).
I think this should work
item = PriceListItem.objects.get(id=tarif_id)
value = getattr(item, price_+settings.CURRENCY)
In case you are only interested in that specific column, you can make the query more efficient with .values_list:
my_price = PriceListItem.objects.values_list_(
'price_{}'.format(settings.CURRENCY),
flat=True
).get(id=tarif_id)
This will only fetch that specific column from the database, which can be a (a bit) faster than first fetching the entire row into memory, and then discard all the rest later.
Here my_price is thus not a PriceListItem object, but the value that is stored for the specific price_cur column.
It will thus result in a query that looks like:
SELECT pricelistitem.price_cur
FROM pricelistitem
WHERE id=tarif_id

Using a Parameter in Expression Transformation

I have a workflow in which I've set up an expression transformation to select $$Param for a particular field, and then within the target properties I've set a delete value. I've tried this by substituting $$Param for a hardcoded value and it works fine, however, for some reason when I put in $$Param, it doesn't actually do the delete. Is there a reason? Am I doing something wrong?
Just for clarification, the workflow executes successfully - no error is thrown but it's not doing what it's supposed to.
Thanks in advance,
$$Param needs to be passed thru a parameter file and you have the option to set an initial value when you declare the parameter in the mapping under Mappings > Parameter and Variables.
Have you looked at the session log to see what's the override value of $$Param is being used? If it's a SQL delete, try to turn see in the session log the query being executed in the database.

Laravel 5/Faker - Factory data changes

Been using Faker in combination with sqLite in-memory database for testing in Laravel lately and I have this strange issue where my model has a factory and everything except the first variable (which happens to also be the primary key of the table) gets set correctly.
Let's explain further, the variable I'm pointing at uses the following rule in the factory:
'kvk' => strval($faker->randomNumber(9)),
So it should become a string containing a 9 digit number. Now the next step is calling the factory from my controller test and I also have another User model which uses this 'kvk' variable from my company as a foreign key reference:
$this->_company = factory(Company::class)->create([ 'address_id' => $this->_address->id ]);
$this->_user = factory(User::class)->create([ 'kvk' => $this->_company->kvk ]);
But when I put an echo $this->_company->kvk; in between, it shows me that 'kvk is set to 1, which should not be possible because of the rule I put in my factory.
Finally the user is used to mock the session in the test and is also used to check wether I should have the rights to edit an address using the following check:
$user = Auth::user();
$company = Company::where('kvk', $user->kvk)->first();
$address = Address::whereId($id)->first();
if($company->address_id != $address->id)
{
return abort(403);
}
So first I get the current logged in user (which is mocked to the created user above, and this works perfectly. Next I get the company associated with this user (which should be the one created above, since I used the company->kvk as a foreign key reference), however when I output the saved Company to my log I see that the kvk is set to a 9 digit string like it's supposed to.
I really can't put my finger on why at first the kvk is set to 1 and afterwards in my test it seems perfectly fine the way it should be, but my test fails because the wrong reference is set in my User so it can't find the right Company. Do you have any idea what could be the reason for this? I've also tried setting the 'kvk' explicitly while creating with the factory, but this also does not work and will stil output 1.
After diving into the source of Laravel, I found out it had to with my models. While I did set the primary key attribute in my models, I forgot to set the auto increment boolean to false. This caused my models to cast the primary key to an auto increment integer every time it was saved to the database. This solved the issue.

rails: convert a string with regex and write to database

I have an application that accepts an address and writes it to the db. I then want to take that address and convert it to something I can send through Google Maps, so I need to replace all the spaces with "+" symbols. I understand how to do that with a regex:
address.gsub(/\s/, "+")
And can create a variable that does it, voila. But I want the converted address to live in the DB as well, so it doesn't have to be processed every time. I'm not sure how I process that when I'm creating the entry to begin with and save it to the db as a separate entity ("gmapaddress" or something).
Thanks!
given a table name of rails_db_table and columns userid and gmapaddress and instance vars #gmapaddress and #userid it's as simple as
UPDATE rails_db_table
SET gmapaddress=#gmapaddress
WHERE userid=#userid
of course a more rails way of doing this is with active_record that allows a construct such as:
#user.update_attribute :gmapaddress, #gmapaddress
#user.save