serialization is not work properly in lower levels- django-rest-framework - django

my Comment model is like below:
class Comment(models.Model):
user = models.ForeignKey(User)
content = models.TextField()
parent = models.ForeignKey('self', related_name='answers')
I want to serializer comment and all of its answers. So i've wrote this serializer:
class CommentSerializer(ModelSerializer):
user = UserSerializer(fields=('get_full_name',), allow_null=True)
class Meta:
model = Comment
fields = ['user', 'content', 'answers']
depth = 10
now when I serialize a comment by this serializer, it works properly just for level 1 of the comment. the answers object shows all the data, such as user's password! or it shows 'parent' while I didn't include parent to the fields of Meta class of the serializer.
the following is the output of this serializer:
{
"data": {
"pk": 15,
"user": null,
"name": "rwqerweqr",
"email": null,
"content": "A",
"approved": false,
"created_at": "2019-03-31T23:16:44.980338+04:30",
"answers": [
{
"id": 14,
"content": "B",
"name": null,
"email": null,
"approved": false,
"created_at": "2019-03-31T23:08:13.374350+04:30",
"user": {
"id": 3,
"is_superuser": true,
"email": "email#gmail.com",
"phone": "+111111111111",
"password": "pbkdf2_sha256$36000$RDdFKTXhvzhb$GaR2Y0p1DBp/5pO5yqayMmdNN/XjnoWEdKshYbmkwCk=",
"first_name": "Alex",
"last_name": "Doe",
"created_at": "2019-03-26T21:16:53.818407+04:30",
"last_login": "2019-03-26T21:17:14.987512+04:30",
"is_active": true,
"is_staff": true,
"groups": [],
"user_permissions": []
},
"parent": {
"id": 15,
"content": "A",
"name": "rwqerweqr",
"email": null,
"approved": false,
"created_at": "2019-03-31T23:16:44.980338+04:30",
"user": null,
"parent": {
"id": 13,
"content": "C",
"name": null,
"email": null,
"approved": false,
"created_at": "2019-03-31T21:27:29.152248+04:30",
"user": {
"id": 3,
"is_superuser": true,
"email": "email#gmail.com",
"phone": "+111111111111",
"password": "pbkdf2_sha256$36000$RDdFKTXhvzhb$GaR2Y0p1DBp/5pO5yqayMmdNN/XjnoWEdKshYbmkwCk=",
"first_name": "Alex",
"last_name": "Doe",
"created_at": "2019-03-26T21:16:53.818407+04:30",
"last_login": "2019-03-26T21:17:14.987512+04:30",
"is_active": true,
"is_staff": true,
"groups": [],
"user_permissions": []
},
"parent": {
"id": 14,
"content": "B",
"name": null,
"email": null,
"approved": false,
"created_at": "2019-03-31T23:08:13.374350+04:30",
"user": {
"id": 3,
"is_superuser": true,
"email": "email#gmail.com",
"phone": "+111111111111",
"password": "pbkdf2_sha256$36000$RDdFKTXhvzhb$GaR2Y0p1DBp/5pO5yqayMmdNN/XjnoWEdKshYbmkwCk=",
"first_name": "Alex",
"last_name": "Doe",
"created_at": "2019-03-26T21:16:53.818407+04:30",
"last_login": "2019-03-26T21:17:14.987512+04:30",
"is_active": true,
"is_staff": true,
"groups": [],
"user_permissions": []
},
"parent": {
"id": 15,
"content": "A",
"name": "rwqerweqr",
"email": null,
"approved": false,
"created_at": "2019-03-31T23:16:44.980338+04:30",
"user": null,
"parent": {
"id": 13,
"content": "C",
"name": null,
"email": null,
"approved": false,
"created_at": "2019-03-31T21:27:29.152248+04:30",
"user": {
"id": 3,
"is_superuser": true,
"email": "email#gmail.com",
"phone": "+111111111111",
"password": "pbkdf2_sha256$36000$RDdFKTXhvzhb$GaR2Y0p1DBp/5pO5yqayMmdNN/XjnoWEdKshYbmkwCk=",
"first_name": "Alex",
"last_name": "Doe",
"created_at": "2019-03-26T21:16:53.818407+04:30",
"last_login": "2019-03-26T21:17:14.987512+04:30",
"is_active": true,
"is_staff": true,
"groups": [],
"user_permissions": []
},
"parent": {
"id": 14,
"content": "B",
"name": null,
"email": null,
"approved": false,
"created_at": "2019-03-31T23:08:13.374350+04:30",
"user": {
"id": 3,
"is_superuser": true,
"email": "email#gmail.com",
"phone": "+111111111111",
"password": "pbkdf2_sha256$36000$RDdFKTXhvzhb$GaR2Y0p1DBp/5pO5yqayMmdNN/XjnoWEdKshYbmkwCk=",
"first_name": "Alex",
"last_name": "Doe",
"created_at": "2019-03-26T21:16:53.818407+04:30",
"last_login": "2019-03-26T21:17:14.987512+04:30",
"is_active": true,
"is_staff": true,
"groups": [],
"user_permissions": []
},
"parent": {
"id": 15,
"content": "A",
"name": "rwqerweqr",
"email": null,
"approved": false,
"created_at": "2019-03-31T23:16:44.980338+04:30",
"user": null,
"parent": {
"id": 13,
"content": "C",
"name": null,
"email": null,
"approved": false,
"created_at": "2019-03-31T21:27:29.152248+04:30",
"user": {
"id": 3,
"is_superuser": true,
"email": "email#gmail.com",
"phone": "+111111111111",
"password": "pbkdf2_sha256$36000$RDdFKTXhvzhb$GaR2Y0p1DBp/5pO5yqayMmdNN/XjnoWEdKshYbmkwCk=",
"first_name": "Alex",
"last_name": "Doe",
"created_at": "2019-03-26T21:16:53.818407+04:30",
"last_login": "2019-03-26T21:17:14.987512+04:30",
"is_active": true,
"is_staff": true,
"groups": [],
"user_permissions": []
},
"parent": {
"id": 14,
"content": "B",
"name": null,
"email": null,
"approved": false,
"created_at": "2019-03-31T23:08:13.374350+04:30",
"user": 3,
"parent": 15
}
}
}
}
}
}
}
}
}
},
{
"id": 16,
"content": "D",
"name": "werwer",
"email": null,
"approved": false,
"created_at": "2019-04-01T18:37:46.981327+04:30",
"user": null,
"parent": {
"id": 15,
"content": "A",
"name": "rwqerweqr",
"email": null,
"approved": false,
"created_at": "2019-03-31T23:16:44.980338+04:30",
"user": null,
"parent": {
"id": 13,
"content": "C",
"name": null,
"email": null,
"approved": false,
"created_at": "2019-03-31T21:27:29.152248+04:30",
"user": {
"id": 3,
"is_superuser": true,
"email": "email#gmail.com",
"phone": "+111111111111",
"password": "pbkdf2_sha256$36000$RDdFKTXhvzhb$GaR2Y0p1DBp/5pO5yqayMmdNN/XjnoWEdKshYbmkwCk=",
"first_name": "Alex",
"last_name": "Doe",
"created_at": "2019-03-26T21:16:53.818407+04:30",
"last_login": "2019-03-26T21:17:14.987512+04:30",
"is_active": true,
"is_staff": true,
"groups": [],
"user_permissions": []
},
"parent": {
"id": 14,
"content": "B",
"name": null,
"email": null,
"approved": false,
"created_at": "2019-03-31T23:08:13.374350+04:30",
"user": {
"id": 3,
"is_superuser": true,
"email": "email#gmail.com",
"phone": "+111111111111",
"password": "pbkdf2_sha256$36000$RDdFKTXhvzhb$GaR2Y0p1DBp/5pO5yqayMmdNN/XjnoWEdKshYbmkwCk=",
"first_name": "Alex",
"last_name": "Doe",
"created_at": "2019-03-26T21:16:53.818407+04:30",
"last_login": "2019-03-26T21:17:14.987512+04:30",
"is_active": true,
"is_staff": true,
"groups": [],
"user_permissions": []
},
"parent": {
"id": 15,
"content": "A",
"name": "rwqerweqr",
"email": null,
"approved": false,
"created_at": "2019-03-31T23:16:44.980338+04:30",
"user": null,
"parent": {
"id": 13,
"content": "C",
"name": null,
"email": null,
"approved": false,
"created_at": "2019-03-31T21:27:29.152248+04:30",
"user": {
"id": 3,
"is_superuser": true,
"email": "email#gmail.com",
"phone": "+111111111111",
"password": "pbkdf2_sha256$36000$RDdFKTXhvzhb$GaR2Y0p1DBp/5pO5yqayMmdNN/XjnoWEdKshYbmkwCk=",
"first_name": "Alex",
"last_name": "Doe",
"created_at": "2019-03-26T21:16:53.818407+04:30",
"last_login": "2019-03-26T21:17:14.987512+04:30",
"is_active": true,
"is_staff": true,
"groups": [],
"user_permissions": []
},
"parent": {
"id": 14,
"content": "B",
"name": null,
"email": null,
"approved": false,
"created_at": "2019-03-31T23:08:13.374350+04:30",
"user": {
"id": 3,
"is_superuser": true,
"email": "email#gmail.com",
"phone": "+111111111111",
"password": "pbkdf2_sha256$36000$RDdFKTXhvzhb$GaR2Y0p1DBp/5pO5yqayMmdNN/XjnoWEdKshYbmkwCk=",
"first_name": "Alex",
"last_name": "Doe",
"created_at": "2019-03-26T21:16:53.818407+04:30",
"last_login": "2019-03-26T21:17:14.987512+04:30",
"is_active": true,
"is_staff": true,
"groups": [],
"user_permissions": []
},
"parent": {
"id": 15,
"content": "A",
"name": "rwqerweqr",
"email": null,
"approved": false,
"created_at": "2019-03-31T23:16:44.980338+04:30",
"user": null,
"parent": {
"id": 13,
"content": "C",
"name": null,
"email": null,
"approved": false,
"created_at": "2019-03-31T21:27:29.152248+04:30",
"user": {
"id": 3,
"is_superuser": true,
"email": "email#gmail.com",
"phone": "+111111111111",
"password": "pbkdf2_sha256$36000$RDdFKTXhvzhb$GaR2Y0p1DBp/5pO5yqayMmdNN/XjnoWEdKshYbmkwCk=",
"first_name": "Alex",
"last_name": "Doe",
"created_at": "2019-03-26T21:16:53.818407+04:30",
"last_login": "2019-03-26T21:17:14.987512+04:30",
"is_active": true,
"is_staff": true,
"groups": [],
"user_permissions": []
},
"parent": {
"id": 14,
"content": "B",
"name": null,
"email": null,
"approved": false,
"created_at": "2019-03-31T23:08:13.374350+04:30",
"user": 3,
"parent": 15
}
}
}
}
}
}
}
}
}
}
]
}
}
as you see, in the answers object, it shows user's data completely, include of its hashed password or permissions.
or it shows parent field while it is not in the serializer at all!.
how to solve this problem?
thanks.
UPDATE
usecase: Comments B and D are answers of comment A. (b.parent == A)

As displayed in django-rest-framework docs:
The depth option should be set to an integer value that indicates the depth of relationships that should be traversed before reverting to a flat representation.
If you want to customize the way the serialization is done you'll need to define the field yourself.
You should serialize the answers field, in order to hide those informations. I would follow the approach of SerializerMethodField. I will write an example:
class CommentSerializer(ModelSerializer):
user = UserSerializer(fields=('get_full_name',), allow_null=True)
answers = serializers.SerializerMethodField()
class Meta:
model = Comment
fields = ['user', 'content', 'answers']
def get_answers(self, instance):
# Here you will create your custom serialization

Related

Django Framework Rest match calendar users with current user

I am doing an exercise where the goal it's to match my current calendar with other users.
To do this, I created a UserProfile App and Schedule App. Each user has a profile that can have multiple intervals.
Considering my current calendar:
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"id": 3,
"user": {
"id": 3,
"username": "john.doe",
"first_name": "John",
"last_name": "Doe"
},
"calendar": [
{
"id": 1,
"mon": true,
"tue": true,
"wed": true,
"thu": true,
"fri": true,
"sat": true,
"sun": true,
"start_date": "09:30",
"end_date": "12:20"
},
{
"id": 2,
"mon": true,
"tue": true,
"wed": true,
"thu": true,
"fri": true,
"sat": true,
"sun": true,
"start_date": "14:00",
"end_date": "23:00"
}
]
}
]}
When I am doing a call to the endpoint /api/search/users it returns all User Profiles with info from each user.
example:
{
"count": 99,
"next": "http://localhost:8000/api/search/users?page=2",
"previous": null,
"results": [
{
"id": 1,
"user": {
"id": 1,
"username": "john.bender.99",
"first_name": "John",
"last_name": "Bender"
},
"calendar": [
{
"id": 2,
"mon": true,
"tue": true,
"wed": true,
"thu": false,
"fri": true,
"sat": false,
"sun": false,
"start_date": "09:30",
"end_date": "12:20"
},
{
"id": 55,
"mon": false,
"tue": true,
"wed": true,
"thu": false,
"fri": true,
"sat": false,
"sun": false,
"start_date": "14:30",
"end_date": "19:20"
}
]
}
]}
Now, what I want to do actually is a search for related users with my calendar to know what days/hours we have a match.
When I do a call to this endpoint /api/search/users?related=self, I want to see this
{
"count": 2,
"results": [
{
"id": 87,
"user": {
"id": 87,
"username": "diana.taller",
"first_name": "Diana",
"last_name": "Taller"
},
"calendar": [
{
"id": 2,
"mon": true,
"tue": true,
"wed": true,
"thu": false,
"fri": true,
"sat": false,
"sun": false,
"start_date": "10:30",
"end_date": "11:20"
},
{
"id": 55,
"mon": false,
"tue": true,
"wed": true,
"thu": false,
"fri": true,
"sat": false,
"sun": false,
"start_date": "16:30",
"end_date": "17:20"
}
]
},{
"id": 128,
"user": {
"id": 128,
"username": "therockjosh",
"first_name": "Josh",
"last_name": "Bail"
},
"calendar": [
{
"id": 2,
"mon": false,
"tue": false,
"wed": false,
"thu": false,
"fri": true,
"sat": false,
"sun": false,
"start_date": "10:30",
"end_date": "11:20"
},
{
"id": 55,
"mon": false,
"tue": false,
"wed": false,
"thu": false,
"fri": true,
"sat": true,
"sun": true,
"start_date": "14:30",
"end_date": "17:00"
}
]
}
]}
The interception between my availability and from users is done between per day and then each interval to see when we have a match.
Inside my Search App, I created this
if related == "self":
current_user_profile = UserProfile.objects.filter(user=self.request.user)
related_users = UserProfile.objects.filter(calendar__in=current_user_profile.calendar.all())
return related_users
If I call current_user_profile, returns me the current user data as I provided you before.
If I call UserProfile.objects.all(), returns me the user's data as I provided you before.
But for some reason, I can't call calendar from current_user_profile.calendar as this image shows.
Is anyone have some idea how could I do this?
I think you need to use get function if you wanna get the object.
if related == "self":
# not UserProfile.objects.filter in order to get the UserProfile object.
current_user_profile = UserProfile.objects.get(user=self.request.user)
related_users = UserProfile.objects.filter(calendar__in=current_user_profile.calendar.all())
return related_users
Here we have the solution I found to exclude my user from the search.
current_user_profile = UserProfile.objects.get(user=self.request.user)
related_users = UserProfile.objects\
.filter(calendar__in=current_user_profile.calendar.all()) \
.exclude(user_id=current_user_profile.id)
return related_users

Unable to serialize backend response in ember data

I am trying to access the pagination variables in the response such as page_number, total_pages inside my controller as I have introduced this feature just now to this legacy code in the backend and now I am stuck how can I read those data and go further.
My serializer code
import DS from 'ember-data';
var underscore = Ember.String.underscore;
export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
rules: {
embedded: 'always'
},
test_cases: {
serialize: "id",
deserialize: 'records'
},
},
keyForRelationship: function (rawKey) {
return underscore(rawKey) + "_id";
}
})
route.js
import Route from '#ember/routing/route';
import { hash } from 'rsvp'
import ResetScrollMixin from 'admin-frontend/mixins/reset-scroll-mixin'
export default Route.extend(ResetScrollMixin, {
model(params) {
return hash({
ruleSetVersions: this.store.query('rule-set-version', {
rule_set_id: params.rule_set_id,
is_archived: false,
page: '1'
}),
ruleSet: this.store.findRecord('rule-set', params.rule_set_id)
})
}
});
And response json looks like
{
"page_number": 1,
"page_size": 5,
"rule_set_versions": [{
"id": 930,
"last_updated_by": 1,
"created_by": 1,
"default_rule_id": 963,
"is_live": true,
"is_archived": false,
"start_time": "2020-12-30T18:30:00Z",
"rule_set_id": 11,
"rules": [{
"id": 963,
"priority": 10000001,
"conditions": [],
"results": [{
"id": 59,
"operator": null,
"value": "00",
"rule_id": 963,
"rule_set_metadatum": {
"created_at": "2020-11-18T10:01:38Z",
"datatype": "integer",
"id": 34,
"name": "edd_result",
"property_category": "result",
"updated_at": "2020-11-18T10:01:38Z",
"validations": {
"max": 99999,
"min": 0
}
},
"created_at": "2020-11-18T10:10:41Z",
"updated_at": "2020-11-18T10:10:41Z"
}],
"created_at": "2020-11-18T10:10:32Z",
"updated_at": "2020-11-18T10:10:32Z"
}, {
"id": 971,
"priority": 1,
"conditions": [{
"id": 57,
"comparator": "equal_to",
"value": "100",
"rule_id": 971,
"rule_set_metadatum": {
"created_at": "2020-11-18T10:01:05Z",
"datatype": "text",
"id": 33,
"name": "edd_con",
"property_category": "condition",
"updated_at": "2020-11-18T10:01:05Z",
"validations": {}
},
"created_at": "2020-12-23T09:05:04Z",
"updated_at": "2020-12-23T09:05:04Z"
}],
"results": [{
"id": 66,
"operator": null,
"value": "100",
"rule_id": 971,
"rule_set_metadatum": {
"created_at": "2020-11-18T10:01:38Z",
"datatype": "integer",
"id": 34,
"name": "edd_result",
"property_category": "result",
"updated_at": "2020-11-18T10:01:38Z",
"validations": {
"max": 99999,
"min": 0
}
},
"created_at": "2020-12-23T09:05:10Z",
"updated_at": "2020-12-23T09:05:10Z"
}],
"created_at": "2020-12-23T09:04:54Z",
"updated_at": "2020-12-23T09:04:54Z"
}],
"created_at": "2020-11-18T10:10:32Z",
"updated_at": "2020-12-23T09:06:04Z"
}, {
"id": 932,
"last_updated_by": 1,
"created_by": 1,
"default_rule_id": 969,
"is_live": true,
"is_archived": false,
"start_time": "2020-11-25T18:30:00Z",
"rule_set_id": 11,
"rules": [{
"id": 969,
"priority": 10000001,
"conditions": [],
"results": [{
"id": 64,
"operator": null,
"value": "1000",
"rule_id": 969,
"rule_set_metadatum": {
"created_at": "2020-11-18T10:01:38Z",
"datatype": "integer",
"id": 34,
"name": "edd_result",
"property_category": "result",
"updated_at": "2020-11-18T10:01:38Z",
"validations": {
"max": 99999,
"min": 0
}
},
"created_at": "2020-11-25T11:53:58Z",
"updated_at": "2020-11-25T11:53:58Z"
}],
"created_at": "2020-11-25T11:53:49Z",
"updated_at": "2020-11-25T11:53:49Z"
}, {
"id": 970,
"priority": 1,
"conditions": [{
"id": 56,
"comparator": "equal_to",
"value": "100",
"rule_id": 970,
"rule_set_metadatum": {
"created_at": "2020-11-18T10:01:05Z",
"datatype": "text",
"id": 33,
"name": "edd_con",
"property_category": "condition",
"updated_at": "2020-11-18T10:01:05Z",
"validations": {}
},
"created_at": "2020-11-25T11:54:11Z",
"updated_at": "2020-11-25T11:54:11Z"
}],
"results": [{
"id": 65,
"operator": null,
"value": "100",
"rule_id": 970,
"rule_set_metadatum": {
"created_at": "2020-11-18T10:01:38Z",
"datatype": "integer",
"id": 34,
"name": "edd_result",
"property_category": "result",
"updated_at": "2020-11-18T10:01:38Z",
"validations": {
"max": 99999,
"min": 0
}
},
"created_at": "2020-11-25T11:54:16Z",
"updated_at": "2020-11-25T11:54:16Z"
}],
"created_at": "2020-11-25T11:54:03Z",
"updated_at": "2020-11-25T11:54:03Z"
}],
"created_at": "2020-11-25T11:53:49Z",
"updated_at": "2020-11-25T11:55:12Z"
}, {
"id": 934,
"last_updated_by": 1,
"created_by": 1,
"default_rule_id": 974,
"is_live": false,
"is_archived": false,
"start_time": "2020-12-30T18:30:00Z",
"rule_set_id": 11,
"rules": [{
"id": 974,
"priority": 10000001,
"conditions": [],
"results": [{
"id": 69,
"operator": null,
"value": "00",
"rule_id": 974,
"rule_set_metadatum": {
"created_at": "2020-11-18T10:01:38Z",
"datatype": "integer",
"id": 34,
"name": "edd_result",
"property_category": "result",
"updated_at": "2020-11-18T10:01:38Z",
"validations": {
"max": 99999,
"min": 0
}
},
"created_at": "2021-03-11T10:55:44Z",
"updated_at": "2021-03-11T10:55:44Z"
}],
"created_at": "2021-03-11T10:55:44Z",
"updated_at": "2021-03-11T10:55:44Z"
}, {
"id": 976,
"priority": 1,
"conditions": [{
"id": 60,
"comparator": "equal_to",
"value": "100",
"rule_id": 976,
"rule_set_metadatum": {
"created_at": "2020-11-18T10:01:05Z",
"datatype": "text",
"id": 33,
"name": "edd_con",
"property_category": "condition",
"updated_at": "2020-11-18T10:01:05Z",
"validations": {}
},
"created_at": "2021-03-12T16:18:04Z",
"updated_at": "2021-03-12T16:18:04Z"
}],
"results": [{
"id": 71,
"operator": null,
"value": "100",
"rule_id": 976,
"rule_set_metadatum": {
"created_at": "2020-11-18T10:01:38Z",
"datatype": "integer",
"id": 34,
"name": "edd_result",
"property_category": "result",
"updated_at": "2020-11-18T10:01:38Z",
"validations": {
"max": 99999,
"min": 0
}
},
"created_at": "2021-03-12T16:18:10Z",
"updated_at": "2021-03-12T16:18:10Z"
}],
"created_at": "2021-03-12T16:17:55Z",
"updated_at": "2021-03-12T16:17:55Z"
}],
"created_at": "2021-03-11T10:55:44Z",
"updated_at": "2021-03-11T10:55:44Z"
}],
"total_entries": 3,
"total_pages": 1
}
I am able to read and parse all the data inside the rule_set_versions array but I am unable to access the pagination params that are out side it.
Ember noob here.

Ember Serialize 'ID'

I'm making an API call in Ember and the API I'm hitting is returning some JSON with 'ID' capitalized, it seems Ember is looking for lowercase 'id.' Everything else should work, so how would I serialize just this one piece of JSON in this instance?
See JSON below.
{
"ID": 2,
"name": "Main Menu",
"slug": "main-menu",
"description": "",
"count": 4,
"items": [
{
"id": 11,
"order": 1,
"parent": 0,
"title": "Contact",
"url": "http://localhost:8888/contact/",
"attr": "",
"target": "",
"classes": "",
"xfn": "",
"description": "",
"object_id": 7,
"object": "page",
"object_slug": "contact",
"type": "post_type",
"type_label": "Page"
},
{
"id": 12,
"order": 2,
"parent": 0,
"title": "About",
"url": "http://localhost:8888/about/",
"attr": "",
"target": "",
"classes": "",
"xfn": "",
"description": "",
"object_id": 5,
"object": "page",
"object_slug": "about",
"type": "post_type",
"type_label": "Page",
"children": [
{
"id": 21,
"order": 3,
"parent": 12,
"title": "About Us – Sub",
"url": "http://localhost:8888/about/about-us-sub/",
"attr": "",
"target": "",
"classes": "",
"xfn": "",
"description": "",
"object_id": 9,
"object": "page",
"object_slug": "about-us-sub",
"type": "post_type",
"type_label": "Page"
}
]
},
{
"id": 13,
"order": 4,
"parent": 0,
"title": "Home",
"url": "http://localhost:8888/home/",
"attr": "",
"target": "",
"classes": "",
"xfn": "",
"description": "",
"object_id": 2,
"object": "page",
"object_slug": "home",
"type": "post_type",
"type_label": "Page"
}
],
"meta": {
"links": {
"collection": "http://localhost:8888/wp-json/wp/v2/menus/",
"self": "http://localhost:8888/wp-json/wp/v2/menus/2"
}
}
}
You can define primaryKey: 'ID' by creating model specific serializer. read more for info https://guides.emberjs.com/v2.13.0/models/customizing-serializers/#toc_ids

Ember Array Iteration

I'm getting my JSON in my model and rendering it fine on the template, however, I'd like to access this array (items) on my template. The goal here is to dynamically render these menu items in the nav bar.
My handlebars looks like this,
{{#each menus as |menu|}}
{{#each menu.items as |items|}}
{{items}}
{{/each}}
{{/each}}
And returns
[object Object] [object Object] [object Object]
Any ideas?
JSON below.
{
"ID": 2,
"name": "Main Menu",
"slug": "main-menu",
"description": "",
"count": 4,
"items": [
{
"id": 11,
"order": 1,
"parent": 0,
"title": "Contact",
"url": "http://localhost:8888/contact/",
"attr": "",
"target": "",
"classes": "",
"xfn": "",
"description": "",
"object_id": 7,
"object": "page",
"object_slug": "contact",
"type": "post_type",
"type_label": "Page"
},
{
"id": 12,
"order": 2,
"parent": 0,
"title": "About",
"url": "http://localhost:8888/about/",
"attr": "",
"target": "",
"classes": "",
"xfn": "",
"description": "",
"object_id": 5,
"object": "page",
"object_slug": "about",
"type": "post_type",
"type_label": "Page",
"children": [
{
"id": 21,
"order": 3,
"parent": 12,
"title": "About Us – Sub",
"url": "http://localhost:8888/about/about-us-sub/",
"attr": "",
"target": "",
"classes": "",
"xfn": "",
"description": "",
"object_id": 9,
"object": "page",
"object_slug": "about-us-sub",
"type": "post_type",
"type_label": "Page"
}
]
},
{
"id": 13,
"order": 4,
"parent": 0,
"title": "Home",
"url": "http://localhost:8888/home/",
"attr": "",
"target": "",
"classes": "",
"xfn": "",
"description": "",
"object_id": 2,
"object": "page",
"object_slug": "home",
"type": "post_type",
"type_label": "Page"
}
],
"meta": {
"links": {
"collection": "http://localhost:8888/wp-json/wp/v2/menus/",
"self": "http://localhost:8888/wp-json/wp/v2/menus/2"
}
}
}

how to apply conditions on JSON response in Jmeter?

Response is as follows,
I have to extract all 'deviceResponseStatus.id' where status="PENDING"
how to implement it using JSON Path or REGEX in Jmeter?
{
"apiResponseStatus": "SUCCESS",
"deviceResponseList": [
{
"apiResponseStatus": "SUCCESS",
"id": 23,
"ownership": "CORPORATE",
"deviceName": "Demimbu",
"deviceType": "MOBILE",
"osType": "ANDROID",
"inTotalStorage": 0,
"inAvailableStorage": 0,
"exTotalStorage": 0,
"exAvailableStorage": 0,
"osVersion": "4.2",
"buildVersion": "1.01.08",
"status": "PENDING",
"isDeleted": false,
"policyIdNames": {
"id": 3,
"name": "Test Policy 3"
},
"deviceGroupIdNames": {
"id": 1,
"name": "Default Group"
},
"userIdName": {
"id": 1,
"name": "Randy Lewis"
},
"lastConnected": 1423810405000,
"createdByIdName": {
"id": 1,
"name": "Randy Lewis"
},
"locked": false,
"traceOn": false,
"userEmail": "mdm.user1#gmail.com",
"enrollmentDate": 1423790255000,
"simNumbers": [
"1236547896"
],
"imeiCodes": [],
"mobileNumbers": [
"4558585858858"
],
"createDate": 1421927250000,
"lastModified": 1423810406000,
"rooted": false,
"roaming": false,
"compliant": false
},
{
"apiResponseStatus": "SUCCESS",
"id": 24,
"ownership": "CORPORATE",
"deviceName": "Photobean",
"deviceType": "MOBILE",
"osType": "ANDROID",
"inTotalStorage": 20941258752,
"inAvailableStorage": 8024924160,
"exTotalStorage": 0,
"exAvailableStorage": 0,
"bluetoothMacAddress": "8C:3A:E3:BE:C2:04",
"wirelessMacAddress": "8c:3a:e3:51:bb:e3",
"model": "LG-D686",
"osVersion": "4.4.2",
"buildVersion": "1.01.09",
"status": "PENDING",
"isDeleted": false,
"policyIdNames": {
"id": 3,
"name": "Test Policy 3"
},
"deviceGroupIdNames": {
"id": 1,
"name": "Default Group"
},
"userIdName": {
"id": 1,
"name": "Randy Lewis"
},
"make": "LGE",
"lastConnected": 1423648831000,
"createdByIdName": {
"id": 1,
"name": "Randy Lewis"
},
"locked": false,
"traceOn": false,
"userEmail": "mdm.user1#gmail.com",
"enrollmentDate": 1423628675000,
"simNumbers": [
"404909008623970"
],
"imeiCodes": [
"359004053152889",
"359004053152871"
],
"mobileNumbers": [
"638847339387"
],
"createDate": 1422435029000,
"lastModified": 1423648831000,
"rooted": true,
"roaming": false,
"compliant": false
},
{
"apiResponseStatus": "SUCCESS",
"id": 25,
"ownership": "CORPORATE",
"deviceName": "Livetube",
"deviceType": "MOBILE",
"osType": "ANDROID",
"inTotalStorage": 0,
"inAvailableStorage": 0,
"exTotalStorage": 0,
"exAvailableStorage": 0,
"osVersion": "4.2",
"buildVersion": "1.01.08",
"status": "APPROVAL_PENDING",
"isDeleted": false,
"policyIdNames": {
"id": 3,
"name": "Test Policy 3"
},
"deviceGroupIdNames": {
"id": 1,
"name": "Default Group"
},
"userIdName": {
"id": 1,
"name": "Randy Lewis"
},
"lastConnected": 1423810522000,
"createdByIdName": {
"id": 1,
"name": "Randy Lewis"
},
"locked": false,
"traceOn": false,
"userEmail": "mdm.user1#gmail.com",
"enrollmentDate": 1423790371000,
"simNumbers": [
"1236547896"
],
"imeiCodes": [],
"mobileNumbers": [
"4558585858858"
],
"createDate": 1422435030000,
"lastModified": 1423810522000,
"rooted": false,
"roaming": false,
"compliant": false
},
{
"apiResponseStatus": "SUCCESS",
"id": 26,
"ownership": "CORPORATE",
"deviceName": "Riffwire",
"deviceType": "MOBILE",
"osType": "IOS",
"inTotalStorage": 0,
"inAvailableStorage": 0,
"exTotalStorage": 0,
"exAvailableStorage": 0,
"status": "PENDING",
"isDeleted": false,
"policyIdNames": {
"id": 3,
"name": "Test Policy 3"
},
"deviceGroupIdNames": {
"id": 1,
"name": "Default Group"
},
"userIdName": {
"id": 1,
"name": "Randy Lewis"
},
"createdByIdName": {
"id": 1,
"name": "Randy Lewis"
},
"locked": false,
"traceOn": false,
"userEmail": "mdm.user1#gmail.com",
"simNumbers": [],
"imeiCodes": [],
"mobileNumbers": [],
"createDate": 1422435030000,
"lastModified": 1423216312000,
"rooted": false,
"roaming": false,
"compliant": false
},
Also, Please suggest any good option if any.
You question is JMeter specific, but i believe it's actually regular Json Path expression. For example, in SoapUI it could be done with
$.deviceResponseList[?(#.status=='PENDING')].id
Probably this could help in some way
JMeter provides JSON Path Extractor via JMeter Plugins Extras With Libs Set
I believe that this is the best way to accomplish your goal.
See Using the XPath Extractor in JMeter guide (scroll down to "Parsing JSON" chapter) for the plugin installation instructions and some sample JSON Path queries.