Skip to content

Instantly share code, notes, and snippets.

View azpwnz's full-sized avatar
🎯

Andriy Zhuk azpwnz

🎯
View GitHub Profile

Keybase proof

I hereby claim:

  • I am zhukandrey on github.
  • I am azpwnz (https://keybase.io/azpwnz) on keybase.
  • I have a public key ASAsaBZQAnbf5vYu51bJO5o-pyV_yh5aZEUjk0fYNQ7wwwo

To claim this, I am signing this object:

@azpwnz
azpwnz / serializers.py
Created May 14, 2017 18:30
Add non-model fields back to validated_data by overwriting the to_internal_value function. Then it will be available in update/create methods
def to_internal_value(self, data):
internal_value = super(SearchPresetSerializer, self).to_internal_value(data)
user = data.get("user")
internal_value.update({
"user": user
})
return internal_value
@azpwnz
azpwnz / index.js
Last active May 10, 2017 12:21
Nested conditional statements is JSX
// In order to use nested contitionals, they must be wrapped into container like array
{ this.state.companies
? <h3>Companies</h3>
: [
(this.state.loading
? <p>Loading...</p>
: null
)
]
}
@azpwnz
azpwnz / views.py
Last active May 9, 2017 07:13
The way to check whether object has ManyToMany or OneToOne field in Django
if hasattr(object, 'some_field'):
pass
@azpwnz
azpwnz / serializers.py
Last active April 25, 2017 08:49
ModelSerializer with custom fields (Django Rest Framework)
class CampaignSerializer(serializers.ModelSerializer):
# companies is many-to-many field. Use StringRelatedField will show names of the companes instead of ids.
companies = serializers.StringRelatedField(many=True)
# format date
created_at = serializers.DateTimeField(format="%Y-%m-%d")
# custom field with count of companies (which is many-to-many field). It use method with the same name: get_<name of the field>(self, obj)
companies_count = serializers.SerializerMethodField()
def get_companies_count(self, obj):
return obj.companies.count()