I have a field owner that is a ForeignKey to User model.
This field is required at the time of creation. But it can not be changed later on.
How to make fields Non-Editable? Is there any other way than creating multiple serializers?
Garage Model
class GarageDetails(models.Model):
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, )
name = models.CharField(_('Garage Name'), max_length=254, blank=False, null=False, unique=True)
price = models.IntegerField(_('Price'), blank=False)
available_from = models.TimeField(_('Available From'), default=datetime.time(6, 00), blank=False)
available_till = models.TimeField(_('Available till'), default=datetime.time(18, 00), blank=False)
description = models.TextField(_('Garage Description'), blank=True, null=True)
create_date = cmodels.UnixTimestampField(_('Date Added'), auto_now_add=True)
update_date = cmodels.UnixTimestampField(_('Date Added'), auto_created=True)
is_available = models.BooleanField(_('Available'), default=True)
Serializer
class UserFKSerializer(serializers.ModelSerializer):
class Meta:
model = get_user_model()
fields = ('id', 'name', 'email', 'mobile')
class GarageSerializer(serializers.ModelSerializer):
owner = UserFKSerializer(many=False, read_only=True)
class Meta:
model = GarageDetails
fields = '__all__'
read_only_fields = ('id', 'owner', 'create_date', 'update_date')
Views
class GarageRegister(generics.ListCreateAPIView):
renderer_classes = (JSONRenderer, )
permission_classes = (IsAuthenticated, )
#csrf_exempt
def post(self, request, *args, **kwargs):
serialize = GarageSerializer(data=request.data)
if serialize.is_valid():
# Create Garage with owner & name
class GarageUpdate(generics.ListCreateAPIView):
renderer_classes = (JSONRenderer, )
permission_classes = (IsAuthenticated, )
#csrf_exempt
def post(self, request, *args, **kwargs):
serialize = GarageSerializer(data=request.data)
if serialize.is_valid():
# Update Garage but can't update create_date, id, owner & name
You could create a different model serializer for each use case (update, create):
specifying that field in read_only_fields in your model serializer:
class MyModelSerializer(serializers.ModelSerializer):
class Meta:
model = MyModel
fields = ('a', 'nother', 'field')
read_only_fields = ('owner',)
for django forms instead you set the disabled field:
class MyModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
form.fields['owner'].widget.attrs['disabled'] = True
You can do this by overriding the "update" method as follows:
def update(self, instance, validated_data):
if 'owner' in validated_data:
del validated_data['owner']
return super().update(instance, validated_data)
This will silently ignore the owner field on updates. If you want to you may instead "raise ValidationError('owner may not be set on updates')" but if you do so you may want to read the model instance and only raise the error if it's actually a change to avoid false positives.
Also, if you're using the python2, the "super" call needs to be "super(GarageSerializer, self)" or some such.
Related
Im new to Django REST framework, but getting the hang of it. Im trying to make a serializer from the Profile Model but i dont know how to pass (def followers and following) into the serializer
This is the Profile Model:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.CharField(max_length=245, null=True)
image = models.ImageField(default='default.png', upload_to='profile_pics')
interests = models.ManyToManyField(Category, related_name='interests_user')
stripe_customer_id = models.CharField(max_length=50, blank=True, null=True)
one_click_purchasing = models.BooleanField(default=False)
def __str__(self):
return f'{self.user.username} Profile'
#property
def followers(self):
return Follow.objects.filter(follow_user=self.user).count()
#property
def following(self):
return Follow.objects.filter(user=self.user).count()
def save(self, force_insert=False, force_update=False, using=None,
update_fields=None):
super().save()
img = Image.open(self.image.path)
if img.height > 300 or img.width > 300:
output_size = (300, 300)
img.thumbnail(output_size)
img.save(self.image.path)
class Follow(models.Model):
user = models.ForeignKey(User, related_name='user', on_delete=models.CASCADE)
follow_user = models.ForeignKey(User, related_name='follow_user', on_delete=models.CASCADE)
date = models.DateTimeField(auto_now_add=True)
old_instance = models.ForeignKey('Follow', blank=True, null=True, on_delete=models.CASCADE, editable=False)
def save(self, *args, **kwargs):
if self.pk is not None:
self.old_instance = Follow.objects.get(pk=self.pk)
super().save(*args,**kwargs)
def __str__(self):
return f"For: {self.user} // id: {self.id}"
This is the serializer I built so far,
But it doesnot work correctly, Error: 'User' object has no attribute 'count (line 40)
class PublicProfileSerializer(serializers.ModelSerializer):
follow_user = serializers.SerializerMethodField(read_only=True)
followers = serializers.SerializerMethodField(read_only=True)
following = serializers.SerializerMethodField(read_only=True)
class Meta:
model = Profile
fields = '__all__'
def get_follow_user(self, obj):
return obj.follow_user
def get_followers(self, obj):
# return obj.user.followers.count()
return FollowSerializer(obj.follow_user.count())
def get_following(self, obj):
# return obj.user.following.count()
return FollowSerializer(obj.user.count()) - Line 40
Use this in your serializer
class ExampleSerializer(serializers.ModelSerializer):
followers= serializers.CharField(source='followers') # the followers at source='follower should be the name of function'
following = serializers.CharField(source='following')
class Meta:
model = Example
fields = '__all__'
the lhs is the name you want to give to variable and the rhs is how you gonna get that value(in this case it's the functions)
Try this,
from rest_framework import serializer
class UserSerializer(serializers.ModelSerializer):
# hack..hack..
class FollowSerializer(serializers.ModelSerializer):
# hack..hack..
class ProfileSerializer(serializers.ModelSerializer):
followers = FollowSerializer(many=True, read_only=True)
following = FollowSerializer(many=True, read_only=True)
# hack..hack..
class Meta:
model = models.Profile
fields = (..., "followers", "following", )
Then you can use it with views, viewsets. If you want to use it manually, use it like this
# Single Instance
serializer = ProfileSerializer(single_instance)
serializer.data # This returns json
# Multiple Instances
serializer = ProfileSerializer(iterable_of_instances, many=True)
serializer.data # This returns json
I have already asked this question but I got no response can you please elaborate how should I proceed. I am trying to create the order but i stuck at logic.
models.py:
class OrderItem(models.Model):
image_number = models.CharField(max_length=20)
title = models.CharField(max_length=20)
image_size = models.CharField(max_length=50)
file_type = models.CharField(max_length=20)
price = models.CharField(max_length=50)
def __str__(self):
return self.title
class Order(models.Model):
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
items = models.ManyToManyField(OrderItem)
start_date = models.DateTimeField(auto_now_add=True)
ordered_date = models.DateTimeField()
ordered = models.BooleanField(default=False)
def __str__(self):
return str(self.user)
serializers.py:
class AddtocartSerializers(serializers.ModelSerializer):
class Meta:
model = OrderItem
fields = ['image_number','title','image_size','file_type','price']
class CartSerializers(serializers.ModelSerializer):
class Meta:
model = Order
fields = ['item',
'start_date',
'ordered_date'
]
views.py:
class AddtocartView(generics.CreateAPIView):
authentication_classes = []
permission_classes = []
pagination_class = None
queryset=OrderItem.objects.all()
serializer_class = AddtocartSerializers
class CartView(generics.ListCreateAPIView):
authentication_classes = []
permission_classes = []
pagination_class = None
queryset=Order.objects.all()
serializer_class = CartSerializers
def create(self, request):
return super(AddtocartView, self).create(request, *args, **kwargs)
urls.py:
path('addtocart/',views.AddtocartView.as_view(),name='addtocart'),
path('cart/',views.CartView.as_view(),name='cart'),
how can i create order after addtocart?
Yes you can do a post request to addtocart endpoint. But there must be logic written in addtocart endpoint to create an order and add OrderItem to the order. Then the order number must be returned to browser so that user can resume shopping / checkout the order.
After receiving the order number you can do some javascript stuff to show user his order details.
First of my suggestion for you don't to use ModelViewSet, because all you need is create and update for you view, for create use CreateAPIView, for update RetrieveAPIView.
you code will be like below:
class OrderItemSerializer(serializers.ModelSerializer)
class Meta:
model = OrderItem
fields = ['id', 'image_number', 'image_size', 'file_type', 'price']
class OrderCreateSerializer(serializers.ModelSerializer)
user = serializers.PrimaryKeyRelatedField(queryset=User.objects.all())
items = serializers.ListField(child=serializers.PrimaryKeyRelatedField(queryset=OrderItem.objects.all(), required=True), required=True)
ordered_date = serializers.DateField()
class Meta:
model = Order
fields = ['user', 'items', 'ordered_date']
def create(self, validated_data):
validated_data['user'] = self.context['request'].user
instance = Order.objects.create(**validated_data)
instance.save()
return instance
class CartView(CreateAPIView):
authentication_classes = []
queryset=Order.objects.all()
serializer_class = OrderCreateSerializer
def create(self, request):
return super(AddtocartView, self).create(request, *args, **kwargs)
ModelViewSet include more code which you don't use in you case. And also you can get user from request, and you can just add
validated_data['user'] = self.context['request'].user
I have an object I would like to be able to make via django-rest-api UI.
This object has a manytomany field that holds other objects on it.
Even though that field is blank param is set to True, I get a response that "this field is requiered".
class Post(models.Model):
title = models.CharField(max_length=100)
slug = models.CharField(max_length=200, null=True)
description = models.CharField(max_length=200, null=True)
content = HTMLField('Content', null=True)
black_listed = models.ManyToManyField('profile_app.Profile', related_name='black_listed_posts', blank=True)
score = models.PositiveIntegerField(null=True, default=0, validators=[MaxValueValidator(100)])
serializers.py:
class PostSerializer(serializers.HyperlinkedModelSerializer):
black_listed = ProfileSerializer(many=True)
read_only = ('id',)
def create(self, validated_data):
self.black_listed = []
class Meta:
model = Post
fields = ('id', 'title', 'slug', 'description',
'content',
'black_listed', 'score')
views.py:
class PostViewSet(ModelViewSet):
serializer_class = PostSerializer
queryset = Post.objects.all()
lookup_field = "slug"
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.black_listed = []
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
As you can see, i tried overriding the create() method on both serializer and viewset, but that didnt work and still gave me that the black_list field is requiered.
What i expected that if the field is not required in the db, then the serializer can set it to None on the creation
what am i missing here?
EDIT:
ProfileSerializer:
class ProfileSerializer(serializers.ModelSerializer):
interests = InterestSerializer(read_only=True, many=True)
class Meta:
model = Profile
fields = ('slug', 'user_id', 'image', 'role', 'work_at', 'interests')
You should provide the required=False argument in your serializer declaration:
class PostSerializer(...):
black_listed = ProfileSerializer(many=True, required=False)
# __________________________________________^
If you want to be able to post null values for this field, you may also add allow_null=True.
i'm fighting with DRF too long so now i must ask question.. How change ForeignKey to another? I have user profile and relation to status model.
models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
ldap_uid = models.CharField(max_length=100, blank=True, null=True, default=None)
redmine_id = models.IntegerField(blank=True, null=True, default=None)
status = models.ForeignKey(Status, models.SET_NULL, blank=False, null=True, default=DEFAULT_STATUS_ID)
location = models.ForeignKey(Location, models.SET_NULL, blank=False, null=True, default=DEFAULT_LOCATION_ID)
online = models.BooleanField(default=False)
class SelectValuesModel(models.Model):
name = models.CharField(max_length=100)
display_name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Meta:
abstract = True
class Status(SelectValuesModel):
pass
class Location(SelectValuesModel):
pass
What is good way to change Profile status to another? I'm trying with something like this without success
views.py
class UserStatusView(viewsets.ViewSet):
def partial_update(self, request, pk=None):
user = User.objects.get(pk=pk)
user_profile = user.profile
new_stauts = Status.objects.get(request.data.status)
serialized_data = ProfileSerializer(user_profile)
if(serialized_data.is_valid()):
serialized_data.save(status=new_stauts)
return Response(serialized_data.errors)
And trying send new id via PATCH. I'm trying tto find solution but no success here too. And how do it good? Make another route for updating Profile status? Or make something like profile/1/update_status/2? Now my routing looks like:
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'redmine', views.RedmineCurrentTaskView, base_name='redmine')
router.register(r'parameters', views.ParametersView, base_name='parameters')
router.register(r'update_status', views.UserStatusView, base_name='update_status')
router.register(r'debug', views.DebugStatus, base_name='debug')
urlpatterns = [
path('', views.index, name='index'),
path('api/', include(router.urls))
]
And serializers.py
class SelectValuesSerializer(serializers.ModelSerializer):
class Meta:
fields = ('pk', 'name', 'display_name')
class LocationSerializer(SelectValuesSerializer):
class Meta(SelectValuesSerializer.Meta):
model = Location
class StatusSerializer(SelectValuesSerializer):
class Meta(SelectValuesSerializer.Meta):
model = Status
class ProfileSerializer(serializers.ModelSerializer):
status = StatusSerializer()
location = LocationSerializer()
class Meta:
model = Profile
fields = ('status', 'location', 'online', 'redmine_id')
class UserSerializer(serializers.ModelSerializer):
profile = ProfileSerializer(read_only=True)
class Meta:
model = User
fields = ('pk', 'first_name', 'profile')
read_only_fields = ('first_name',)
Just pass request.data to the serializer with partial=True argument:
class UserStatusView(viewsets.ViewSet):
def partial_update(self, request, pk=None):
user = User.objects.get(pk=pk)
user_profile = user.profile
serialized_data = ProfileSerializer(user_profile, data=request.data, partial=True)
if serialized_data.is_valid():
serialized_data.save()
return Response(serialized_data.data)
return Response(serialized_data.errors)
You need to provide status_id with request body like this:
{"status": 1}
UPD
To pass status as id change your serializer to this:
class ProfileSerializer(serializers.ModelSerializer):
location = LocationSerializer()
class Meta:
model = Profile
fields = ('status', 'location', 'online', 'redmine_id')
def to_representation(self, instance):
self.fields['status'] = StatusSerializer()
return super(ProfileSerializer, self).to_representation(instance)
This allows to post status_id, but get status details with your API.
I'm adding a 'tests' field to my 'Sample' model, where 'tests' will be a list of 'TestRequest' objects. Currently, I'm getting this error:
Got AttributeError when attempting to get a value for field `tests` on serializer `SampleSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `Sample` instance.
Original exception text was: 'Sample' object has no attribute 'tests'.
'tests' is not a field on my model. I'm just trying to add it to the serialized data. Currently, I can get a nested serializer to work for 'klass' but that is because it's defined in the model.
Models:
class Sample(models.Model):
name = models.CharField(max_length=50, null=False, blank=False)
comments = models.TextField(null=True, blank=True)
klass = models.ForeignKey('samples.Batch', null=True, blank=True,
related_name='samples', verbose_name='Batch')
product = models.ForeignKey('customers.Product', blank=False)
NOTRECEIVED = 'nr'
RECEIVED = 'rc'
DISCARDED = 'dc'
DEPLETED = 'dp'
SAMPLE_STATUS = (
(NOTRECEIVED, 'not received'),
(RECEIVED, 'received'),
(DISCARDED, 'discarded'),
(DEPLETED, 'depleted'),
)
status = models.CharField(
max_length=2, choices=SAMPLE_STATUS, default=NOTRECEIVED)
is_recycling = models.BooleanField(default=False)
is_submitted = models.BooleanField(default=False)
received_date = models.DateTimeField(
_('date received'), null=True, blank=True)
class TestRequest(models.Model):
client = models.ForeignKey('customers.Client')
company = models.ForeignKey('customers.Company')
sample = models.ForeignKey('samples.Sample')
procedure_version = models.ForeignKey('registery.ProcedureVersion')
replicates = models.PositiveIntegerField(editable=True, null=True, blank=True)
created_date = models.DateTimeField('Date created', auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)
comments = models.TextField('Comments', blank=True)
Serializers:
class TestSerializer(serializers.ModelSerializer):
href = serializers.HyperlinkedIdentityField(lookup_field='pk', lookup_url_kwarg='pk', read_only=True, view_name='samples_api:test-detail')
class Meta:
model = TestRequest
fields = ('id', 'href',)
class SampleBatchSerializer(serializers.ModelSerializer):
href = serializers.HyperlinkedIdentityField(
lookup_field='pk', lookup_url_kwarg='batch_pk', read_only=True, view_name='samples_api:batch-detail')
class Meta:
model = Batch
fields = ('id', 'href',)
class SampleSerializer(serializers.ModelSerializer):
tests = TestSerializer(many=True)
klass = SampleBatchSerializer(many=False)
class Meta:
model = Sample
# list_serializer_class = FilteredListSerializer
fields = ('id', 'name', 'tests', 'klass',)
def create(self, validated_data):
...
def update(self, instance, validated_data):
...
Viewsets:
class TestRequestViewSet(viewsets.ModelViewSet):
"""
Viewset for the TestRequest model
"""
serializer_class = TestRequestSerializer
def get_queryset(self):
client = get_object_or_404(Client, user=self.request.user)
return TestRequest.objects.filter(company=client.company)
def perform_create(self, serializer):
# Override default creatation to provide request based information.
client = get_object_or_404(Client, user=self.request.user)
company = client.company
serializer.save(client=client, company=company)
class SampleViewSet(viewsets.ModelViewSet):
"""
Viewset for the Sample model
"""
serializer_class = SampleSerializer
def get_queryset(self):
client = get_object_or_404(Client, user=self.request.user)
return Sample.objects.filter(klass__company=client.company)
I would rather not have to add the field to the model. A 'Sample' can have many 'TestRequest's but a 'TestRequest' can only have one 'Sample'.
How do I get my serializer to add the 'tests' field that isn't in the model?
in your SampleSerializer. You have specified 'tests' which is not in your Sample class in your model...
Use nested SerializerMethodField as below....
tests = serializers.SerializerMethodField()
def get_tests(self, obj):
var=TestRequestSerializer(obj.id)
return var.data