I have two field in my django model they should be editable only if user have selected 'type' as 'Dimention' otherwise they should not be visible to user.
My model is look like this code
from django.db import models
class Configuration(models.Model):
name = models.CharField(max_length=30)
user_defined_name = models.CharField(max_length=50)
FieldTypes = (('aD', 'Dimension'), ('aM', 'Measure'))
type = models.CharField(max_length=11, choices=FieldTypes)
is_key = models.BooleanField(default=False, editable=False)
unit = models.CharField(max_length=30, null=True, blank=True, editable=False)
def __str__(self):
return self.name
I know it is possible by using JavaScript but, I don't want to write html or js myself,Thus Can't use JavaScript for doing this.
A pure Django-only way to achieve this is to simply reset the fields from your ModelForm if type is not equal to Dimension. This will appear like magic/unintended behavior; so be careful of the implementation.
For example (assuming you are using the admin interface: the same is valid for a custom ModelForm View):
class ConfigurationAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
# At this point; the object already has the new values set; we will have to reset as needed
conditional_editable_fields = ['is_key', 'unit']
config_type = form.cleaned_data.get('type')
if config_type != 'aD':
for field in conditional_editable_fields:
if field in form.changed_data: # The value has been changed by the user
setattr(obj, field, form.initial.get(field)) # Set the initial value
self.message_user(request, "Cannot edit field: {}; value has been reset".format(field), messages.WARNING) # Inform the user that we reset the value
return super(ConfigurationAdmin, self).save_mode(request, obj, form, change)
I use similar approach for this.
I it works great
In My admin.py
`
fieldsets = (
(None, {
'fields': ('name', 'user_defined_name', 'type', 'is_active')
}),
('Advanced', {
'classes': ('toggle',),
'fields': ('is_kpi', 'unit'),
})
)
actions = [disable_multiple_column, activate_multiple_column]
class Media:
js = ("jquery.js", "my_code.js",)`
I use that JS file to show and hide .
`$(document).ready(function(){
show_hide();
$('#id_type').change(function(){
show_hide();
});
function show_hide(){
if ($("#id_type").val() == 'aM' ){
$(".module")[1].style.display="block"
}
else{
$(".module")[1].style.display="none"
}
}
});`
And in case use already entered values and then change Choice of type or from some other reason these hidden field still have data. I override the save Method of models.py
`
def save(self, *args, **kwargs):
if self.type != 'aM':
self.is_kpi = False
self.unit = None
super(Configuration, self).save(*args, **kwargs)
`
Related
I'm trying to make an attendance system in the frontend I retrieve a list of users which the request.user can take attendance of, I'm using CreateAPIView but this won't get me the desired effect as I want the request.user to be able to toggle between Absent, Present, and on_leave even after the entry has been created on the first request
i have seen questions and answers about create or update here but i couldn't use them so any help would be appriciated
this is my view.py
class AttendanceListCreateAPIView(CreateAPIView):
permission_classes = [IsTeacher_Student]
queryset = Attendance.objects.all()
serializer_class = AttendanceSerializer
def post(self, request, *args, **kwargs):
user = request.user
data = request.data
serializer = AttendanceSerializer(data=data)
if serializer.is_valid():
data_user = serializer.validated_data['user']
## validation
return Response({"message": "Something is wrong, maybe you have already taken attendance for this user"},
status=status.HTTP_400_BAD_REQUEST)
my serializer.py
class AttendanceSerializer(serializers.ModelSerializer):
date = serializers.HiddenField(default=timezone.now)
leave_reason = serializers.CharField(required=False, default="")
class Meta:
model = Attendance
fields = ['user', 'presence', 'leave_reason', 'date']
extra_kwargs = {
'user': {'required': True},
'presence': {'required': True},
'leave_reason': {'required': False},
}
validators = [
UniqueForYearValidator(
queryset=Attendance.objects.all(),
field='user',
date_field='date',
message=("You have already taken the attendance")
)
]
def create(self, validated_data):
instance = Attendance.objects.create(
user=validated_data['user'],
presence=validated_data['presence'],
leave_reason=validated_data['leave_reason'],
date=validated_data['date'],
)
instance.save()
return instance
desired effect
the user fk and date are unique together meaning that if the date isn't unique for the user update it if it is create a new entry
I have a many-to-many relationship in my DB design and I am having trouble with the POST aspect. I currently have a table called Loads, Containers, and Container_Loads (this is the intermediary table).
My question is this:
I want to be able to send a POST request into the ContainerLoad intermediary table and just update that table with the values it requires which are: Load ID (PK of the Load table), Container ID (PK of the Container Table) and # of pallets (unique field to the intermediary table). I am able to GET/retrieve the records just fine, but when I try to send a POST request with a payload such as
{
"id":3,
"pallets":"4",
"containerNumberId":5,
"loadNumberId":53
}
(where containerNumberID and loadNumberID are the existing keys in their respective tables), it seems that my code wants to create a whole new Load entry as well (as it asks me for the remaining fields of the Load model), where as I just want to create an entry in the intermediary table without creating a new entry in the Load table.
So for the purpose of my project, a load can be on many containers [imagine that it's split because all of it couldn't fit on one] and a container can belong to many loads.
My models.py looks like this:
class ContainerLoad(models.Model):
id = models.AutoField(primary_key=True)
load_number = models.ForeignKey(Load,on_delete=models.CASCADE)
container_number = models.ForeignKey(Container,on_delete=models.CASCADE)
pallets = models.CharField(blank=True,null=True,default=0,max_length=20)
class Meta:
db_table = 'ContainerLoad'
#load model shortened for brevity
class Load(models.Model):
id = models.AutoField(primary_key=True)
bnsf_container_number = models.ManyToManyField(Container, through='ContainerLoad',through_fields=('load_number','container_number'))
class Meta:
db_table = "Load"
class Container(models.Model):
id = models.AutoField(primary_key=True)
container_number = models.CharField(max_length=15)
in_use = models.BooleanField()
class Meta:
db_table = "Container"
my serializers.py currently looks like this, the commented out section is from me attempting to get the POST to work)
class ContainerLoadSerializer(WritableNestedModelSerializer):
# load_number_id = LoadSerializer(read_only=False)
# container_number_id = ContainerSerializer(read_only=False)
class Meta:
model = ContainerLoad
fields = "__all__"
depth = 2
class LoadSerializer(WritableNestedModelSerializer):
primary_driver = DriverSerializer(read_only=False)
second_driver = DriverSerializer(allow_null=True,read_only=False)
third_driver = DriverSerializer(allow_null=True,read_only=False)
bnsf_container_number = ContainerSerializer(read_only=False)
pickup_location = LocationSerializer(read_only=False)
delivery_location = LocationSerializer(read_only=False)
broker = BrokerSerializer(read_only=False)
booked_by = EmployeeSerializer(read_only=False)
class Meta:
model = Load
fields = '__all__'
depth = 1
class ContainerSerializer(serializers.ModelSerializer):
container_number = serializers.CharField()
in_use = serializers.BooleanField()
class Meta:
model = Container
fields = '__all__'
depth = 1
And finally the views.py
class ContainerLoadViews(APIView):
def get(self, request, id=None):
if id:
container = ContainerLoad.objects.get(id=id)
serializer = ContainerLoadSerializer(container)
return Response({"status": "success", "data": serializer.data}, status=status.HTTP_200_OK)
else:
containers = ContainerLoad.objects.all()
serializer = ContainerLoadSerializer(containers, many=True)
return Response({"status": "success", "data": serializer.data}, status=status.HTTP_200_OK)
def post(self, request):
serializer = ContainerLoadSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response({"status": "success", "data": serializer.data}, status=status.HTTP_200_OK)
else:
return Response({"status": "Error", "data": serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
If you look at the code of the save method of the BaseSerializer class you will see this:
if self.instance is not None:
self.instance = self.update(self.instance, validated_data)
assert self.instance is not None, (
'`update()` did not return an object instance.'
)
else:
self.instance = self.create(validated_data)
assert self.instance is not None, (
'`create()` did not return an object instance.'
)
How you are not passing the instance in the post function:
serializer = ContainerLoadSerializer(data=request.data)
The save is always calling to create. You should do something like.
try:
instance = ContainerLoad.object.get(id=request.data['id'])
except:
instance = None
serializer = ContainerLoadSerializer(instance=instance, data = request.data)
You have depth is set to 2 in your ContainerLoadSerializer Meta class, which is telling the serializer to generate a nested representation of your models.
https://www.django-rest-framework.org/api-guide/serializers/#specifying-nested-serialization
The default ModelSerializer uses primary keys for relationships, but you can also easily generate nested representations using the depth option:
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 remove the depth attribute, the serializer should default back to expecting a primary key value, which is your desired behaviour.
Your serializer should look something like this:
class ContainerLoadSerializer(serializers.ModelSerializer):
class Meta:
model = ContainerLoad
fields = "__all__"
The solution to this was that I needed a nested response when reading the data from the ContainerLoad table but a simple write (not nested) function when POSTing the data.
The solution was to use the to_representation and to_internal_value methods (https://www.django-rest-framework.org/api-guide/serializers/#overriding-serialization-and-deserialization-behavior) available within DRF to override the behavior of the serializers. Here is the code that now works for both GET and POST requests and it is no longer asking me for fields related to the Load or Container models when inserting data.
class ContainerSerializer(serializers.ModelSerializer):
container_number = serializers.CharField()
in_use = serializers.BooleanField()
class ContainerFieldSerializer(serializers.Field):
def to_internal_value(self,value):
return Container.objects.get(id=value)
def to_representation(self,instance):
return ContainerSerializer(instance=instance).data
class Meta:
model = Container
fields = '__all__'
depth = 1
I did the same for the Load Serializer.
and then for my ContainerLoad Serializer I just assign the FK fields to the new classes I created:
class ContainerLoadSerializer(serializers.ModelSerializer):
cl_container = ContainerSerializer.ContainerFieldSerializer()
cl_load = LoadSerializer.LoadFieldSerializer()
class Meta:
model = ContainerLoad
fields = "__all__"
depth = 2
Let's say we have this model:
class Creation(models.Model):
title = models.CharField(max_length=DEFAULT_LENGTH)
url = models.CharField(max_length=DEFAULT_LENGTH)
date = models.DateTimeField('date published', default=datetime.date.today)
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT)
def __str__(self):
return self.title
And let's use this view:
def submit(request):
FormSet = modelformset_factory(Creation, fields=('title', 'url'))
if request.method == 'POST':
formset = FormSet(request.POST, request.FILES)
if formset.is_valid():
obj = formset.save(commit=False)
obj.author = request.user
obj.save()
else:
formset = FormSet
return render(request, 'app/submit.html', {'formset': formset})
I saved formset to obj and added request.user and committed it to the database.
This doesn't work, because now django throws an error which says
'list' object has no attribute 'author'
which makes perfectly sense, because there is no author in the fields-list above.
But if I add 'author' to fields, another selectbox would be displayed while rendering the template via {{formset}}.
Maybe I could code my own template-code instead of using {{formset}} and omit the author, but I feel that there must be a more elegant way with Django3.0. Is there any?
All I want to do is to get the foreignkey author (see model above) filled with the logged in user.
Turns out that something is wrong with modelformset_factory(..).
If you use a proper CreationForm in models.py:
class CreationForm(ModelForm):
class Meta:
model = Creation
fields = ['title', 'url']
and use it in your views.py:
formset = CreationForm(request.POST)
if request.method == 'POST':
if formset.is_valid():
f = formset.save(commit=False)
f.author = request.user
f.save()
it works.
Trying send valid POST request.
It's works when i send like this:
{
"name":"but",
"sklad":1
}
Response is something like this, all is valid:
{
"name": "but",
"getsklad": "fence"
}
But when I'm trying send this, with 'fence':
{
"name":"but",
"sklad":"fence"
}
Got error:
Invalid type. The primary key value was expected, str was received.
Have this piece of code in models.py:
class Sklad(models.Model):
storages = (
('fence', 'Забор'),
('sett', 'Тротуарка'),
)
storage = models.CharField(
max_length=10,
choices=storages,
help_text='Name of storage',
default='sett'
)
class Zabor(models.Model):
sklad = models.ForeignKey(Sklad, on_delete=models.CASCADE)
name = models.CharField(max_length=10)
...
This in serializers.py:
class ZaborPostSerializer(serializers.ModelSerializer):
class Meta:
model = Zabor
fields = (
...
'name',
'sklad',
)
And Views.py part:
class ZaborView(APIView):
def post(self, request):
serializer = ZaborPostSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response({'status': 'Success'})
else:
return Response({'status': 'Failed'})
I know what when i write '1' in request, this means 'pk'. But how I can send name rather than the id/pk?
I am beginner. So don't kick me hard for stupid questions)
Since you are providing the Sklad FOREIGN_KEY, which is unique you can filter by that value only with one type: integer, string, etc.
You can set the storage field of Sklad to the primary key:
class Sklad(models.Model):
storages = (
('fence', 'Забор'),
('sett', 'Тротуарка'),
)
storage = models.CharField(
max_length=10,
choices=storages,
help_text='Name of storage',
default='sett',
primary_key=True
)
Now you can directly filter with your string value.
Another option is to filter in the view:
from django.shortcuts import get_object_or_404
from django.http import Http404
class ZaborView(APIView):
def post(self, request):
dataDict = dict(request.data)
try:
sklad = get_object_or_404(Sklad.objects, storage=dataDict['sklad'][0])
serializer = ZaborPostSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response({'status': 'Success'})
else:
return Response({'status': 'Failed'})
except Http404:
return Response({'status': 'Failed'})
Note that it will only work if you have only one Sklad with the given storage field.
If you have multiple ones you can pick the first one, since you do not supply a primary key and you have not other fields to filter:
from django.shortcuts import get_list_or_404
.....
sklad = list(get_list_or_404(Sklad.objects, storage=dataDict['sklad'][0]))[0]
.....
Hope it helps !
I have extended Django default User model (just to save user state_id) class with following model.
class UserProfileInfo(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE,)
state_id = models.IntegerField(blank=False)
def __str__(self):
return self.user.username
And now I want to filter the data by logged user's state_id. I tried to implement queryset filter but it does not do anything.
class VisVisitsAdmin(admin.ModelAdmin):
list_per_page = 10
list_display = ('visit_id','visit_no','user_name','mobile_number','program_name','state_name','district_name','block_name','school_name',)
list_filter = ('date_of_visit',)
def queryset(self, request):
qs = super(VisVisitsAdmin, self).get_queryset(request)
if request.user.is_superuser:
return qs
return qs.filter(state_id=request.user.UserProfileInfo.state_id)
def state_name(self, obj):
if obj.school_program:
return obj.school_program.school.cluster.block.district.state.name_of_state
state_name.short_description = 'state name'
You are trying to fetch the state_id from the userprofileinfo instance related to user object.
Everything seems fine but the instance is available as userprofileinfo and not UserProfileInfo , so change qs as :
qs.filter(state_id=request.user.userprofileinfo.state_id)