Sunday, February 28, 2021

Re: Class Base View Foreign key

What's that, am new please

On Sat, Feb 27, 2021, 4:47 PM Rob Wilkinson <wilkyconsultants@gmail.com> wrote:
This is what my code looks like and it now works :

see where it says "example of setting pk..." below:

def dyn_menu_new(request):

    from .models import menu as menuX

    from .models import buttons

    if request.method == 'POST':

        if request.user.is_superuser:

           super_user = "superuser"

        else:

           super_user = ""

        form = dyn_menuForm(request.POST,name=request.user.username,super_user=super_user)


        if form.is_valid():

            menu_id          = request.POST.get('menu_id', '')

            menu_id          = menu_id.replace(" ", "")

            menu_title       = request.POST.get('menu_title', '')

            menu_notes       = request.POST.get('menu_notes', '')

            menu_fb          = request.POST.get('menu_fb', '')

            menu_link        = request.POST.get('menu_link', '')

            menu_align       = request.POST.get('menu_align', '')

            menu_order       = request.POST.get('menu_order', '')

            menu_status      = request.POST.get('menu_status', '')

            menu_admin       = request.POST.get('menu_admin', '')

            menu_image       = request.POST.get('menu_image', '')

            #

            # example of setting pk : https://groups.google.com/g/django-users/c/PcSDKZhPVmc

            #

            buttons_pk = request.POST.get('menu_dropdown')

            menu_dropdown = buttons.objects.get(pk=buttons_pk)

            #

            menu_new_window  = request.POST.get('menu_new_window', '')

            menu_scope  = request.POST.get('menu_scope', '')

            current_user = request.user

            menu_user  = current_user


            p = menuX(menu_id=menu_id, menu_title=menu_title, menu_notes=menu_notes, menu_fb=menu_fb, menu_link=menu_link, menu_align=menu_align, menu_order=menu_order, menu_status=menu_status, menu_admin=menu_admin, menu_image=menu_image, menu_dropdown=menu_dropdown, menu_new_window=menu_new_window,menu_scope=menu_scope,menu_user=menu_user)

            try:

               p.save()

            except:


               messages.error(request, "Menu ID is not unique. Please specify a different Menu ID.")

               return render(request, 'dyn_menu.html', {'form': form})

            return redirect("/theme/dyn_menu_list")


On Sat, Feb 27, 2021 at 10:40 AM sebasti...@gmail.com <sebastian.jung2@gmail.com> wrote:
Hey,

thanks for you response. Before i save i want to make is_valid() and there came this exception.

Regards

wilkycon...@gmail.com schrieb am Samstag, 27. Februar 2021 um 16:36:35 UTC+1:
I had similar issues, I found this helpful:


On Sat, Feb 27, 2021 at 9:19 AM Ryan Nowakowski <tub...@fattuba.com> wrote:
I think choices is causing the problem. Try limit_choices_to instead:

https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.limit_choices_to

On February 27, 2021 8:06:26 AM CST, "sebasti...@gmail.com" <sebasti...@gmail.com> wrote:
Hello,

I have a CBV with Createview. Now i have a Foreign Key from Box to Tabs model. When i submit request then i get error: Cannot assign "1": "Box.tabs_link" must be a "Tabs" instance. This happens on form.is_valid

I understand this error but i don't know how can i fix this. 

Models.py:
class Box(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=200, default="", blank=False, null=False) #Überschrift der Box
    tabs_link = models.ForeignKey(Tabs, on_delete=models.CASCADE, null=False,blank=False,choices=[
                                (c.id, c.name) for c in Tabs.objects.all()]
                                   )

    column = models.PositiveIntegerField(choices=column_choices,blank=False, null=False,default=0) #linke oder rechte Seite
    position = models.PositiveIntegerField(default=1,blank=False, null=False) #position auf der linken/rechten Seite
    modul = models.PositiveIntegerField(choices=Modul_Auswahl,
                                        default=0)  

    class Meta:
        ordering = ["position","column"]
    def __str__(self):
        return self.name

Views.py:
class BoxCreateView(CreateView):
    model = Box
    template_name = 'marketing/boxcreate.html'
    form_class = Boxform
    success_url = reverse_lazy('boxlist')

    def post(self, request, *args, **kwargs):
        formbox = Boxform(self.request.POST)
        if (formbox.is_valid()):
           pass

Forms.py
class Boxform(forms.ModelForm):
    class Meta:
        model = Box
        exclude = ('',)

        labels = {
            'tabs_link': 'Tabulator',
            'column': 'Spalte',
        }
        widgets = {
            'name': textinputfeld,
            'position': integerfeld,
            'column': integerfeld,
            'modul': selectfield,
            'tabs_link': selectfield,
        }

Regards


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/C48208D2-7C9F-4F99-B664-20269F331FD2%40fattuba.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3a53be95-069b-434c-bdfa-b71c0d5fd9c5n%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAAdSEeamDUxsGD3qmpBYq-kB34%3DaMTDUApe0WC3NM25Osoy4wQ%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CADkdEzELut3N2nofznxrN%3D%2BAQ1ef-YtsWE5ynv7TJDrnAqX5ig%40mail.gmail.com.

No comments:

Post a Comment