form need shows from the models.py - django-models

every one ,,I have
models.py
......
class Place(models.Model):
name = models.CharField(max_length=100, blank=True, null=True)
release = models.DateTimeField(blank=True, null=True)
def __unicode__(self):
return self.name
class ProductsTbl(models.Model):
......
place = models.ManyToManyField(Place)
......
def __unicode__(self):
return self.name
and I have forms.py here
forms.py
from django.forms import ModelForm
from .models import *
class ProductsTblForm(ModelForm):
class Meta:
model = ProductsTbl
fields = ('place',)
......
however,,I need let my templates shows the 'release' in form,,right now,,it only shows the 'name' which under the Class place(models.Model): but no 'release',,how can I let the 'release' can show up?

You can access model instance variables in the form by using form.instance.<field_name>.

Related

Django Rest Framework: Form field populated with object representation after validation error

After the form is validated the field is populated with object representation from memory.
Form screenshot
I am using MoneyField in my Product model from django-money library.
models.py
class Product(models.Model):
CURRENCY_CHOICES = (
('EUR', 'EUR'),
('USD', 'USD'),
('GBP', 'GBP'),
('PLN', 'PLN')
)
price = MoneyField(max_digits=10, decimal_places=2, blank=False,
currency_choices=CURRENCY_CHOICES)
serializers.py
class ProductSerializer(serializers.ModelSerializer):
price_currency = serializers.ChoiceField(choices=Product.CURRENCY_CHOICES)
class Meta:
model = Product
fields = (
'price','price_currency',
)
views.py
class ProductList(generics.ListCreateAPIView):
serializer_class = ProductSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
queryset = Product.objects.all()
I would really appreciate any suggestions on why this is happening.

How to Implement One to Many in DRF

I am Designing a Model
class Timer(models.Model):
total_time = models.FloatField(default=5)
date_time = models.DateTimeField(auto_now_add=True)
class WatchTiming(models.Model):
user = models.OneToOneField("authentication.User", on_delete=models.CASCADE, primary_key=True)
current_timer = models.ForeignKey(Timer, on_delete=models.CASCADE, related_name="current_timer")
previous_timers = models.ForeignKey(Timer, on_delete=models.CASCADE, related_name="previous_timers")
and serializer for this model is
from rest_framework import serializers
from .models import Timer, WatchTiming
class TimerSerializer(serializers.ModelSerializer):
class Meta:
model = Timer
exclude = ("id",)
class WatchTimingSerializer(serializers.ModelSerializer):
current_timer = TimerSerializer(required=False)
previous_timers = TimerSerializer(many=True, read_only=True)
user = serializers.PrimaryKeyRelatedField(read_only=True)
class Meta:
model = WatchTiming
fields = "__all__"
def create(self, validated_data):
watch_timing = WatchTiming.objects.create(user=self.context["request"].user, current_timer=Timer.objects.create())
return watch_timing
WatchTiming is a table that is used to store user watch time
current_timer stores today's timer
when the day expires current_timer values are added in the previous_timer and the current_timer value is replaced with the default
Now My issue is how can I create one to many relationships, I already have written relationships but its not working
I have been stuck on this for 4 consecutive days.
You can use a subserializer, just like you did with your TimerSerializers:
from rest_framework import serializers
from .models import Timer, WatchTiming
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User # authentication.User
fields = (
'id',
'username',
)
class WatchTimingSerializer(serializers.ModelSerializer):
current_timer = TimerSerializer(required=False)
previous_timers = TimerSerializer(read_only=True)
user = serializers.UserSerializer(read_only=True)
class Meta:
model = WatchTiming
fields = '__all__'
def create(self, validated_data):
return WatchTiming.objects.create(
user=self.context['request'].user,
current_timer=Timer.objects.create(),
)
Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.
Note: The related_name=… parameter [Django-doc]
is the name of the relation in reverse, so from the Timer model to the WatchTiming
model in this case. Therefore it (often) makes not much sense to name it the
same as the forward relation. You thus might want to consider renaming the current_timer relation to watch_timings.
By using SerializerMethodField you can create one to many relationships.
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = "__all__"
class WatchTimingSerializer(serializers.ModelSerializer):
current_timer = TimerSerializer(required=False)
previous_timers = TimerSerializer(many=True, read_only=True)
user = serializers.SerializerMethodField(read_only=True)
class Meta:
model = WatchTiming
fields = "__all__"
def get_user(self, obj):
data = User.objects.filter(id=obj.id)
return UserSerializer(data, many=True).data

Filtering the results in the dropdown for foreign key in Django rest framework

I have 3 models. User, process, processmapping. process model has a created_by filed which is a foreign key to user model. processmapping model has processname filed which is a foreign key to process table.
My code is as follows.
models.py
class process(models.Model):
created_by = models.ForeignKey(User, on_delete=models.CASCADE, db_column= "username")
process_name = models.CharField(max_length=200)
....
class processmap(models.Model):
process = models.ForeignKey(process, on_delete=models.CASCADE, db_column="process_name")
....
views.py
class processViewSet(viewsets.ModelViewSet):
permission_classes = (IsAuthenticated,)
queryset= process.objects.all()
serializer_class = processSerializer
filter_backends = (DjangoFilterBackend,filters.SearchFilter)
filter_fields = ('created_by', 'process_name',)
def get_queryset(self):
if self.request.user.is_superuser:
return self.queryset
else:
queryset = self.queryset
query_set = queryset.filter(created_by=self.request.user)
return query_set
class processmapViewset(viewsets.ModelViewSet):
permission_classes = (IsAuthenticated,)
try:
queryset= processmap.objects.all()
serializer_class = processmapSerializer
filter_backends = (DjangoFilterBackend,filters.SearchFilter)
filter_fields = ('process', 'service', 'sequence','process__created_by')
Serializers.py
class processSerializer(serializers.HyperlinkedModelSerializer):
#created_by = UserSerializer(many=True)
class Meta:
model = process
fields = ("__all__")
def perform_create(self, serializer):
serializer.save(user=self.request.user)
class processmapSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = processmap
fields = ("__all__")
def perform_create(self, serializer):
serializer.save(user=self.request.user)
When a user creates a process and then goes to mapping the process, the foreign key lists all the processes that are present in the process table. Instead, I need to list down only the processes that are created by the user logged in. How can I achieve this.
Note: I have searched for the same and most answers involve forms. Please note I'm not using any forms and I want that logic to be implemented in the views.py.
Thanks in advance :)

how to insert data into database using forms in Django

models.py
class Author(models.Model):
author_id = models.AutoField(primary_key=True)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
age=models.IntegerField()
class Meta:
db_table=u'Author'
def __unicode__(self):
return u"%d %s %s %s %d" % (self.pk, self.first_name, self.last_name, self.email,self.age)
class Book(models.Model):
book_id=models.AutoField(primary_key=True,unique=True)
book_name=models.CharField(max_length=30)
publisher_name=models.CharField(max_length=40)
author=models.ForeignKey(Author)
class Meta:
db_table = u'Book'
def __unicode__(self):
return u'%d %s %s' % (self.pk, self.book_name, self.publisher_name)
my forms.py
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = ['author_id','first_name','last_name','email','age']
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields=['book_id','book_name','publisher_name','author_id']
views.py
def addbook(request):
log.debug("test....")
form = BookForm
if request.POST:
form = BookForm(request.POST)
if form.is_valid():
form.save()
return render_to_response('addbook.html',{'form': form} , context_instance=RequestContext(request))
I am trying to insert the data into database and displaying it through a template.But i ma not sure the code what i used are correct becaues i am getting lots of error.eg recently i got the error as"ValueError: ModelForm has no model class specified.".
But after rectifying this error again errors are coming and not able to save the data to database.
Please check the code's are correct,if not give me the correct code for doing the same.Acutally i think some problem in the views.py.Please check everything and get me the cooect one.
Thanks.

No ForeignKey exception Django

We have this model which has a foreign key to user:
class Medic(models.Model):
user = models.ForeignKey(User)
first_name = models.CharField(max_length=100, blank=True)
middle_name = models.CharField(max_length=100, blank=True)
last_name = models.CharField(max_length=100, blank=True)
while in admin.py, we tried to filter the Users that are already registered shown on Medic page to be User that only belong to group called 'staff':
from django.contrib import admin
from django.contrib.auth.models import User
from profiles.models import Medic
class StaffMed(admin.StackedInline):
model = User
def queryset(self, request):
qs = super(StaffMed, self).queryset(request).filter(group__name='staff')
return qs
class MedicAdmin(admin.ModelAdmin):
inlines = [StaffMed]
#model = Medic
admin.site.register(Medic, MedicAdmin)
and then we get this exception:
class 'django.contrib.auth.models.User' has no foreignKey to class 'staff.models.Medic'
You have to specify the model which you have the Foreign key in:
class StaffMed(admin.StackedInline):
model = MedicAdmin
Then you need to filter like this:
def queryset(self, request):
qs = super(StaffMed, self).queryset(request).filter(user.group__name='staff')
return qs

Resources