raise TypeError(repr(o) + " is not JSON serializable - odoo-8

Can help me? i want to domain many2one field where id not show in other transaction
#api.multi
#api.onchange('batch_id')
def _onchange_batch_id(self):
if self:
tempt=[]
for record in self:
tempt.extend([record.batch_id])
culling = self.env['estate.nursery.cullinglinebatch'].search([('batch_id', '!=', list(tempt))])
return {
'domain': {'batch_id': [('batch_id','not in',culling),('qty_abnormal','>',0)]}
}

In ODOO8/9 search method always return object not the Id of object.
culling = self.env['estate.nursery.cullinglinebatch'].search([('batch_id', '!=', list(tempt))])
Here culling is the object of model 'estate.nursery.cullinglinebatch'
Your domain should be look like
'domain': {'batch_id': [('batch_id','not in',culling.ids),('qty_abnormal','>',0)]}
here i have uses culling.ids instead of culling.
I hope this will help you.

Related

Query Builder filter for multi level deep relationship in Laravel

I have a selection of plots which each belong to a development by a hasManyThrough relationship through housetypes. I want to filter these by development on their overview page. Plots has a housetype_id column and housetypes has a development_id column.
public function plots()
{
return $this->hasManyThrough(Plot::class, Housetype::class);
}
When I use my filter it returns the developments ID number as $development, I then need this to only show plots which are linked to that development.
I have looked into using whereHas or Join methods but have been unable to figure this out. Current filter scope is below. Thanks
public function scopeFilterDevelopment($query)
{
$development = request()->input('filter_development');
if ($development == "") {
return;
}
if(!empty($development)){
$query->where('development_id', $development);
}
}
If I can understand it right you wish to assert a condition on other Model, HasMany will load all the objects to the related model once the query is completed. Eloquent then binds the related model objects to each.
Try joins from Laravel instead. I feel this is what you exactly want: https://laravel.com/docs/5.8/queries#joins
I would use whereHas to filter the relationship:
YourModel::whereHas('plots', function($query) {
$query->filterDevelopment();
})->get();
I would also edit the query scope not to rely on the request global function, but instead pass the development of value as a parameter.
you have make a leftjon and then use when, you dont have to use
if(!empty($development)){
$query->where('development_id', $development);
}
this any more, you can use
->when($development=="" ? false : true, function($query) use ($development){
return $query->where('development_id', $development);
})
this is a full example
$queryBuilder = DB::table('facturas')->
leftJoin('clientes','clientes.id','=','facturas.clientes_id')->
select('facturas.estados_id as estado','facturas.numero as
numero',DB::raw('concat(clientes.nombre," ",clientes.apellido) as cliente'))->
when($estados===null ? false: true,function($query) use ($estados){
return $query->whereIn('facturas.estados_id', $estados);
})
It was a whereHas that solved this in the end! (another developer at work walked me through this)
Relationship -
public function housetype()
{
return $this->belongsTo(Housetype::class);
}
Function -
public function scopeFilterDevelopment($query)
{
if (request()->input('filter_development') == "") {
return;
}else{
$query->whereHas('housetype', function($housetype){
$housetype->where('development_id', request()->input('filter_development'));
});
}
}
This then returns any plot where its housetype has a matching development_id for the filter_development from the request.
Thanks for everyone's input

django rest framework ordering non model field(serializer field)

i want to ordering on my fields like this:
class DealerBackOfficeViewSet(mixins.ListModelMixin,
mixins.RetrieveModelMixin,
mixins.CreateModelMixin,
mixins.UpdateModelMixin,
viewsets.GenericViewSet):
filter_backends = (filters.OrderingFilter,
)
ordering_fields = ('online',...)
this way of ordering work only on model's fields but online field defined in my serializer and while test in postman not work.
i want to done it like this :
class CustomOrdering(filters.OrderingFilter):
def filter_queryset(self, request, queryset, view):
params = request.query_params.get(self.ordering_param)
if params == 'online':
... my serializer codes
return super(CustomOrdering, self).filter_queryset(request, queryset, view)
this problem is other fields ordering not work!! is there a way to solve it any way?
if related docs help me please give me the link .
thanks for your site
after struggle in this challenge i undrestand that exist a way to some how indicate this fields as model field and not need to CustomOrdering and any extra codes!
in my get_queryset function i change the code:
queryset = Dealer.objects.all()
to:
queryset = Dealer.objects.all().annotate(bids_count=Count('bid'), device_count=Count('device'))
note that this two fields in my serializer not in my model.
in my serilizer change this two field from SerializerMethodField to IntegerField and clean the defs.
then in my api file add this:
filter_backends = (filters.OrderingFilter,)
ordering_fields = ('bids_count', 'device_count')
this my last serializer:
class DealerListSerializer(serializers.ModelSerializer):
device_count = serializers.IntegerField()
bids_count = serializers.IntegerField()
class Meta:
model = Dealer
fields = ('id', 'last_name', 'first_name', 'username', 'person_trust', 'is_active',
'work_type', 'address', 'mobile', 'device_count', 'online', 'bids_count')
by this way my code is very clear and my CustomOrdering and all elif statements also clean!
It doesn't work because the fields defined in your serializer aren't part of the model. The ordering attribute only works for model fields. You'd probably have to introduce a work around like creating a dynamic field using annotations and then order using that field but this depends on whether or not your online field logic can be annotated.

Laravel Relationship with WHERE clause returns nothing

I have areas. Each area have N curses
So, each curse belongsTo only one area.
My class
class Area extends Model
{
public function curses()
{
return $this->hasMany('App\Curse');
}
}
My Controller
public function getCursesByAreaId()
{
$areaId = Input::get('areaId'); //ID of selected Area
//area_id is a field of table CURSE. A FK to Area.
$areas = Area::with('curses')->where('area_id', $areaId)->get();
foreach($areas as $area)
{
var_dump($areas);
}
}
Curse Model
class Curseextends Model
{
protected $fillable =
[
'description',
'area_id'
];
}
Using the laravel's debugbar, I see that the query being executed is this:
select * from "areas" where "area_id" = '2'
Isn't it supose to run the relational query ? (with the joins)
Already checked if I'm receiving the right id and it's ok.
the problem is that it's bringing no data at all.
You need something like the following:
$areas = Area::whereHas('curses', function($query) use ($areaId) {
$query->where('area_id', $areaId);
})
->with('curses')
->get();
The whereHas is required only if you want to get the areas that has matching curses.
But, I think you can do it like this way if you only need the related curses because one curse only belong to a single area so, if have the area then you'll get all the curses attached to it:
$area = Area::with('curses')->find($areaId); // Single area
// Access the curses
$curses = $area->curses; // collection of curses
I think you are using relational area_id which is present in curse table, try retrieving the ID of area by id not by area_id, something like this:
$areas = Area::where('id', $areaId)->with('curses')->get();
Hope you get the solution.
Try this :
$areas = Area::whereHas('curses', function($area_query) use ($area_id) {
$area_query->where('area_id', $area_id)
})
->with('curses')
->get();

Load more attributes into existing eloquent model instance

Is it possible to load additional attributes into model instance without multiple queries or hackery? Let me explain:
// I got a tiny model with only id loaded
$model = Model::first(['id']);
// Then some code runs
// Then I decide I'd need `name` and `status` attributes
$model->loadMoreAttributes(['name', 'status']);
// And now I can joyously use name and status without additional queries
$model->name;
$model->status;
Does Eloquent have something similar to my fictional loadMoreAttributes function?
Notice kindly that I'm not a novice and am very well aware of Model::find($model->id) and such. They're just too wordy.
Thanks for your attention in advance.
You may extend the Eloquent model to have this loadMoreAttributes method like so:
use Illuminate\Database\Eloquent\Model;
class YourModel extends Model
{
public function loadMoreAttributes(array $columns)
{
// LIMITATION: can only load other attributes if id field is set.
if (is_null($this->id)) {
return $this;
}
$newAttributes = self::where('id', $this->id)->first($columns);
if (! is_null($newAttributes)) {
$this->forceFill($newAttributes->toArray());
}
return $this;
}
}
This way you can do this on your model:
$model = YourModel::first(['id']);
$model->loadMoreAttributes(['name', 'status']);
LIMITATION
However there's a limitation to this hack. You may only call loadMoreAttributes() method if the unique id of your model instance is already fetched.
Hope this help!

Doctrine Query Builder - How to select a many-to-many relationship?

I have a form in Symfony2 that needs to have an entity choice field filled with a collection of Players in a particular tournament. I create the form Type and pass the tournament ID to the query_builder property of the correct field. In the entity repository I have this method:
public function allPlayersInTournamentQuery($tournament_id)
{
$repo = $this->getEntityManager()->getRepository('GameBundle:Tournament');
$tournament = $repo->find($tournament_id);
$players = $tournament->getPlayers();
$playersIds = array();
foreach ($players as $player) {
$playersIds[] = $player->getId();
}
$playersQuery = $this->createQueryBuilder('p')
->in('p.id', $playersIds)
->orderBy('p.real_name', 'ASC');
return $playersQuery;
}
The function in() does not exist in the query builder. I hope the method shows what I am trying to do. Im tying to return the query builder that selects the correct players found in the given tournament.
How can I achieve this?
Thanks!
You can use the helper methods provided by the query builder $playersQuery->expr()->in('p.id', $playersIds)
Your query will be something like that:
$playersQuery = $this->createQueryBuilder('p');
$playersQuery->where($playersQuery->expr()->in('p.id', $playersIds))
->orderBy('p.real_name', 'ASC');
More information about the helper methods here

Resources